David_N

Hi,

I am wondering if it is possible to fill a polygon with a gradient. I have tied using the PathGradientBrush but I cant make it preform the way I would like it to.

Bitmap bmTest = new Bitmap(200,200);
Graphics g = Graphics.FromImage(bmTest);

Point[] points = new Point[] { new Point(10, 10), new Point(10, 100), new Point(100, 100), new Point(100, 90), new Point(20, 90), new Point(20, 20), new Point(100, 20), new Point(100, 10) };
PathGradientBrush br = new PathGradientBrush(points);

g.FillPolygon(br, points);
PictureBox1.Image = bmTest;


This is what I get:


this is what I want (photoshop-ed):



ie, I want the gradient to follow the polygon around, instead of it acting with a sort of centerpoint for the polygons bounding-box.

An alternative to this woud ofcorse be to brake the polygon up into rectangles, but if possible I would like to avoid that...

Thanks in advance.

/David N.


Re: Windows Forms General Drawing gradients

Figo Fei - MSFT

Hi, David

Try to use LinearGradientBrush instead of PathGradientBrush

For example:

            LinearGradientBrush lb = new LinearGradientBrush(new Point(10, 10), new Point(10, 100), Color.Black, Color.White);

           g.FillPolygon(lb, points);

----Modified according to your code.

Thanks






Re: Windows Forms General Drawing gradients

David_N

Thanks but thats not quite right. It has to be at is darkest at the top right corner and then go left, then down, then right and be at it brightest in the lower right corner. ie, the gradient does not go straight top-to-bottom.

Is this possible




Re: Windows Forms General Drawing gradients

Figo Fei - MSFT

Hi, David

You can simply draw thrice, each time use a LinearGradientBrush to fill a rectangle.

Thanks