views:

225

answers:

2

I have polygons of various shapes and sizes. They have a solid fill and currently a solid border.

I would like to give the polygons a gradient on their edge to soften them.

So far I've tried using a Pen with a LinearGradientBrush and whilst the effect it produces is very interesting it's most definitely not what I want ;)

I've looked through the System.Drawing.Drawing2D namespace but there didn't seem to be any other classes that would be applicable for this purpose.

I've had a search around and the articles that I can find are mostly about creating borders for rectangles, which are mush easier, or are irrelevant.

So to summarize, does anyone have a way of drawing a gradient border in on a polygon using GDI+?

+1  A: 

Perhaps a screen shot of what your previous attempt produced and a mock up of what you would like would help?

Though I suspect the issue you're running into is that the direction and offset of the gradient is consistent throughout the entire shape and does not change with the orientation of the lines of the polygon.

Have you taken a look instead at the PathGradientBrush? (Examples) If you can't achieve the effect using it with a Pen for the stroke of the shape, perhaps you could do it with two "fills" with the first (the border) being slightly larger than the second (the interior).

Josh Einstein
Yep, the issue with the LinearGradientBrush the gradient stays on the same alignment and so doesnt follow the edge of the polygon.The PathGradientBrush might possibly do the job, but it's generally for putting a gradient inside a polygon rather than on the boarder, so to use it I'd have to employ an algorithm to enlarge the polygon enough to encorperate the border inside it's bounds.I'm currently looking at using the PathGradientBrush but it's giving me an "out of memory" exception on a 3 point polygon which isnt encouraging.I shall persevere, thanks for the suggestion :)
AndyJ
A: 

I think I have done exactly what you're asking for, but in my case I have used it for creating soft shadows on text.

I do the following:

  1. Draw the text (in your case: polygon) to a Bitmap
  2. Apply a softening filter on the alpha channel only
  3. Iterate step 2 as many times needed to get the desired gradient width
  4. Finally draw the result onto the resulting bitmap/screen
danbystrom