Sma*_*oat 6 c# graphics winforms
如何在Winform中的文本上添加阴影。特别是,在位图对象上绘制文本。我知道我们可以用深色绘制文本并将其移到正确的位置以使其像阴影一样。但是这个阴影看起来是如此渺茫和牢固。我希望它更宽且更模糊。我发现一些可以模糊和成像的功能。但是,当我适应我的情况时,它将透明区域变为黑色。请给我一个指南。
作为渲染模糊阴影的一种替代方法,更性能友好的选项可能是将阴影稍微向右下方偏移(如您最初建议的那样),但要具有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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
举例说明如何从代码中调用它,例如在OnPaint方法中:
RenderDropshadowText(e.Graphics, "Dropshadow Text",
this.Font, Color.MidnightBlue, Color.DimGray, 64, new PointF(10, 10));
Run Code Online (Sandbox Code Playgroud)
为了使内容更加整洁,并获得更具说服力的阴影效果,我们可以修改上述功能以模拟模糊效果,方法是稍稍向左,向右稍稍绘制透明度的文字投影的:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是结果输出的屏幕截图: