Shadow shadow in text in c # winform

How to lower a shadow on the text in winform. Especially, draw text on a raster object. I know that we can paint this text in dark color and put it in the right position to make it shadow. But this shadow seems so thin and hard. I want it to be wider and blurry. I found some features that can blur and mimic. But when I turn to my situation, it turns the transparent region into black. Please give me a guide.

+4
source share
3 answers

As an alternative to rendering a blurry shadow, a more user-friendly option would be to render the shadow slightly shifted down and to the right (as you originally intended), but with alpha transparency so that the shadow does not seem so β€œhard”:

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 will be called from code, for example, in the OnPaint method:

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

To nudge things a bit and get a more convincing shadow effect, we could modify the above function to simulate the blur effect by slightly drawing text with additional alpha transparency, once to the left and once slightly to the right of the 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 final result:

enter image description here

+4
source

You can try using Path (if you can create a path from 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 image overlay (this is just an idea, here is an example of creating something luminous, 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); } } 
+1
source

I know the answer may not be useful at all, but if its just static text uses an image instead

0
source

Source: https://habr.com/ru/post/1499158/


All Articles