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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With