7

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.

IMPENTA
  • 379
  • 1
  • 6
  • 18
  • relevant: http://stackoverflow.com/questions/7364026/algorithm-for-fast-drop-shadow-in-gdi?rq=1 – Rotem Aug 27 '13 at 14:07

4 Answers4

7

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:

enter image description here

Lemonseed
  • 1,644
  • 1
  • 15
  • 29
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);
            }
        }
Sinatr
  • 20,892
  • 15
  • 90
  • 319
0

Here's a very simplified implementation of a WinForms Label Control that creates shadows:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace MyCustom.Controls
{
    public class ShadowLabel : Label
    {
        #region Properties
        protected int _xOffset = 5;
        protected int _yOffset = 5;
        #endregion

        #region Constructor
        public ShadowLabel() : base() => InitializeComponent();
        #endregion

        #region Accessors
        /// <summary>Specifies the solid-colour value of the shadow. No alpha information from this setting is used.</summary>
        /// <remarks>Alpha blending is handled programmatically via the <i>Alpha</i> accessor value.</remarks>
        /// <seealso cref="Alpha"/>
        public Color ShadowColor { get; set; } = Color.Black;

        /// <summary>Specifies the vertical translation of the shadow (up/down). Range: -25 to +25.</summary>
        /// <remarks>Using a negative value shifts the shadow up, while a positive value shifts downwards.</remarks>
        public int xOffset
        {
            get => this._xOffset;
            set => this._xOffset = (value < 0) ? Math.Max(value,-25) : Math.Min( 25, value );
        }

        /// <summary>Specifies the horizontal translation of the shadow (left/right). Range: -25 to +25.</summary>
        /// <remarks>Using a negative value shifts the shadow left, while a positive value goes right.</remarks>
        public int yOffset
        {
            get => this._yOffset;
            set => this._yOffset = (value < 0) ? Math.Max( value, -25 ) : Math.Min( 25, value );
        }

        /// <summary>Specifies the starting Alpha value of the shadow (how solid is it).</summary>
        /// <remarks>The shadow is made more transparent as it deepens, from this value to zero.</remarks>
        public byte Alpha { get; set; } = 255;
        #endregion

        #region Methods
        protected override void OnPaint( PaintEventArgs e )
        {
            Graphics g = e.Graphics;
         
            int xStart = Math.Min( this.Location.X, this.Location.X + xOffset ),
                xEnd = Math.Max( this.Location.X, this.Location.X + xOffset ),
                yStart = Math.Min( this.Location.Y, this.Location.Y + yOffset ),
                yEnd = Math.Max( this.Location.Y, this.Location.Y + yOffset ),
                steps, xIncrement, yIncrement, alphaIncrement;
        
            steps = Math.Max( xEnd - xStart, yEnd - yStart );
            xIncrement = ( xOffset  < 0 ? -1 : 1 ) * (int)Math.Floor( (xEnd - xStart) / (float)steps );
            yIncrement = ( yOffset  < 0 ? -1 : 1 ) * (int)Math.Floor( (yEnd - yStart) / (float)steps );
            alphaIncrement = (int)Math.Floor( Alpha / (float)steps );

            if ( steps > 0 )
            {
                for ( int i = steps; i > 0; i-- )
                    g.DrawString(
                        this.Text,
                        this.Font,
                        new SolidBrush(
                                Color.FromArgb(
                                    this.Alpha - (alphaIncrement * i),
                                    ShadowColor.R,
                                    ShadowColor.G,
                                    ShadowColor.B
                                )
                            ),
                            new PointF()
                            {
                                X = (xIncrement * i), // this.Location.X + (xIncrement * i), 
                                Y = (yIncrement * i)  // this.Location.Y + (yIncrement * i) 
                            }
                        );

                g.DrawString( this.Text, this.Font, new SolidBrush( this.ForeColor ), new PointF( 0f, 0f ) );
            }
            else base.OnPaint( e );
        }
        #endregion

        /// <summary>Required designer variable.</summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>Clean up any resources being used.</summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose( bool disposing )
        {
            if ( disposing && (components != null) )
                components.Dispose();

            base.Dispose( disposing );
        }

        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() =>
            components = new System.ComponentModel.Container();
        #endregion
    }
}
NetXpert
  • 511
  • 5
  • 14
-1

I know that answer may not behelpful at all but if its just static text use an image instead