Introduction
I develop a flow charting software for the company I work for and I am always looking for a way to spruce up the application. After getting the nitty gritty down in the application, I turned to the little stuff that users will look at or use and think, awe that's nice. So when it came down to the look of the flow charting object, I wanted a drop shadow to show some depth to the drawing canvas. I searched around and everything that I found on the internet was using an image and skewing or stretching it and drawing it under the object to give it a shadow. So I started drawing the shadow for the object with a transparent color, but there was the problem. It looked ok but the edges were way too sharp, that's when I came up with this solution. Hope you like it and use it - Larry
Overview
Here, I am going to show you a method that I found for making Drop Shadows for GDI+ drawn Shapes that have fuzzy edges. First, we need to set the distance that the shadow should be drawn from the shape. In this example I use a NumericUpDown
control so that we can change this distance and see what distance looks best.
_ShadowDistance = 0f - (float)nmDistance.Value;
picCanvas.Invalidate();
Now that we have setup the distance let's get to the drawing. Here, I will not go over what was done to draw the object, only the shadow. So at this point, we have a GraphicsPath()
that has been created to draw our rounded rectangle. So now, we need to create the drop shadow and this is how it's done.
using(PathGradientBrush _Brush = new PathGradientBrush(_Path))
{
_Brush.WrapMode = WrapMode.Clamp;
ColorBlend _ColorBlend = new ColorBlend(3);
_ColorBlend.Colors = new Color[]{Color.Transparent,
Color.FromArgb(180, Color.DimGray),
Color.FromArgb(180, Color.DimGray)};
_ColorBlend.Positions = new float[]{0f, .1f, 1f};
_Brush.InterpolationColors = _ColorBlend;
e.Graphics.FillPath(_Brush, _Path);
}
It's very simple to do but provides a much better presentation of a shadow then just drawing with a slightly transparent brush. I hope that this helps somebody out there.