7

winform如何给文字添加阴影。特别是在位图对象上绘制文本。我知道我们可以用深色绘制该文本并将其带到正确的位置以使其像阴影一样。但这个影子似乎是那么的纤细和坚实。我希望它更宽更模糊。我发现了一些可以模糊和图像的功能。但是当我应用到我的情况时,它会将透明区域变成黑色。请给我一个指导。

4

3 回答 3

7

作为渲染模糊阴影的替代方法,一个对性能更友好的选项可能是渲染阴影略微向下和向右偏移(如您最初建议的那样),但具有alpha 透明度,以便阴影看起来不是所以“坚实”:

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);
        }
    }
}

举例说明如何从代码中调用它,例如在OnPaint方法中:

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

为了稍微修饰一下,获得更令人信服的阴影效果,我们可以修改上面的函数来模拟模糊效果,方法是稍微绘制具有更大 alpha 透明度的文本,一次向左,一次向右阴影:

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);
    }
}


这是结果输出的屏幕截图:

在此处输入图像描述

于 2016-02-07T20:35:09.600 回答
1

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);
            }
        }
于 2013-08-27T14:32:10.827 回答
-1

我知道答案可能根本没有帮助,但如果它只是静态文本,请改用图像

于 2013-08-27T14:17:51.457 回答