dotNoted

Icon

Observations of .Net development in the wild

A use for the PathGradientBrush!

I’ve found it!

Ok, I was hasty and somewhat petulant about the PathGradiantBrush. I admit I was not restrained in my hour of roiling frustration with the confounded thing when skewering both it and it’s author for a lack of appreciation of the finer points of connecting same. But let’s be frank: apparently nobody uses it because it is a rough beast which is not only too difficult to use for what limited benefit obtained, but also horrendously underdocumented.

Nevertheless, there is something it is good at: radial blends. The reason for this is that it has a CenterColor property. This fact struck me in answer to a mental question (scream for effect) "How do I get the center of this shape shaded?" The CenterColor property allows a two-tone blend in a circle shape, thus simluating a shadow when a shade of gray is used at the middle and transparent everywhere else. Translate and elongate this shape a little, and you get a good dropshadow when you are drawing your own objects. See pic for effect. The following code should get you close:

GraphicsPath path = new GraphicsPath();

path.AddEllipse(aRectangle);

shadowBrush = new PathGradientBrush(path);

shadowBrush.CenterColor = SystemColors.ControlDarkDark;

shadowBrush.SurroundColors = new Color[] { Color.Transparent };

shadowBrush.CenterPoint = new PointF(aRectangle.Width/2, aRectangle.Height/2);

Filed under: .Net Graphics