简体   繁体   中英

Drop shadow on text in C# winform

How to drop shadow on text in winform. Especially, draw text on a bitmap object. I know we can draw that text with a dark color and bring to the right position to make it like a shadow. But this shadow seems so slim and solid. I want it wider and blurred. I found some functions that can blur and image. But when I apply to my situation, it turns the transparent area to black. Please give me a guide.

As an alternative to rendering a blurred shadow, a more performance-friendly option might be to render the shadow slightly offset down and to the right (as you initially suggested), but with an alpha-transparency so that the shadow does not appear to be so "solid":

protected void RenderDropshadowText(
    Graphics graphics, string text, Font font, Color foreground, Color shadow, 
    int shadowAlpha, PointF location)
{
    const int DISTANCE = 2;
    for (int offset = 1; 0 <= offset; offset--)
    {
        Color color = ((offset < 1) ? 
            foreground : Color.FromArgb(shadowAlpha, shadow));
        using (var brush = new SolidBrush(color))
        {
            var point = new PointF()
            {
                X = location.X + (offset * DISTANCE),
                Y = location.Y + (offset * DISTANCE)
            };
            graphics.DrawString(text, font, brush, point);
        }
    }
}

To give an example of how this would be called from code, such as in an OnPaint method:

RenderDropshadowText(e.Graphics, "Dropshadow Text", 
    this.Font, Color.MidnightBlue, Color.DimGray, 64, new PointF(10, 10));

To spruce things up a bit, and get a more convincing shadow effect, we might be able to modify the above function to simulate a blur effect by drawing the text with further alpha transparency slightly, once to the left, and once slightly to the right of the drop shadow:

if (offset > 0)
{
    using (var blurBrush = new SolidBrush(Color.FromArgb((shadowAlpha / 2), color)))
    {
        graphics.DrawString(text, font, blurBrush, (point.X + 1), point.Y);
        graphics.DrawString(text, font, blurBrush, (point.X - 1), point.Y);
    }
}


Here is a screenshot of the resultant output:

在此输入图像描述

You can try to use Path (if you can produce a path out of a text?) and PathGradientBrush

        using (PathGradientBrush brush = new PathGradientBrush(pathShadow))
        {
            ColorBlend blend = new ColorBlend();
            blend.Colors = new Color[] { Color.Transparent, Color.Black };
            blend.Positions = new float[] { 0.0f, 1.0f };
            brush.InterpolationColors = blend;
            graph.FillPath(brush, pathShadow);
        }

Or you can try to do something with the overlay image (it's just an idea, here is an example of making something glowing defined by path ):

        // inside OnPaint
        // overlay
        using (Bitmap bmp = new Bitmap(Width, Height, PixelFormat.Format32bppArgb))
        {
            using (Graphics gtemp = Graphics.FromImage(bmp))
            {
                // fake glowing
                using (LinearGradientBrush brush = new LinearGradientBrush(ClientRectangle, Color.FromArgb(200, 255, 255, 255), Color.FromArgb(0, 0, 0, 0), LinearGradientMode.Vertical))
                {
                    brush.SetBlendTriangularShape(0.5f, 1.0f);
                    gtemp.FillPath(brush, path);
                }
                // draw on screen
                e.Graphics.DrawImage(bmp, 0, 0);
            }
        }

我知道答案可能没有任何帮助,但如果它只是静态文本而是使用图像

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM