Bug in PathGradientBrush

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I'm fairly new to C# graphics, but I think I've found a bug in PathGradientBrush
The following code *should* show a hollowed rectangle. In fact, there is an indentation on the left edge

Can someone experienced comment

Regard

Tony Reynold

code follows.........

// Set up a GraphicsPath of 2 nested rectangle
RectangleF rect1 = new RectangleF(0,0,300,200)
RectangleF rect2 = new RectangleF(50,50,200,100)

GraphicsPath path = new GraphicsPath();
path.AddRectangle(rect1);
path.AddRectangle(rect2)
path.CloseFigure();

// Set up a PathGradientBrush, use Aqua for start and finish colours to show bug clearl
PathGradientBrush pathGrBrush = new PathGradientBrush(p1)
Color[] colors = {Color.FromArgb(255, 0, 255, 255)}
pathGrBrush.SurroundColors = colors
pathGrBrush.CenterColor = Color.FromArgb(255, 0, 255, 255)

// Fill the outlin
e.Graphics.FillPath(pathGrBrush, path);
 
Hi Tony,
No, there is no bug. This is the way PathGradientBrush works.
I think the name of the brush is somehow misleading it should be called
PolygonGradientBrush. Because the points you provide to the constructor
defines a polygon. Eventhough, there is constructor overload that accepts a
path it uses the path to extract the points (GraphicsPath.PathPoints) and
treats these points as vertices of a polygon. When the brush is used to fill
out some area it circles around the points of the polygon edges and draws
the gradient from the points to the center of the brush.
In your example you skipped the code for *p1* that you use for creating the
brush. I assume this is the same path you are trying to fill because it has
the effect you are describing. If you draw a line connecting all points in
the path (path.PathPoints) you will see the polygon used by the brush. You
can see as well the strange vertex that apears as a result of moving form
the last point of the path's outer rectangle (Left-Bottom point) to the
first point of the inner rectangle (Left-Top point) and respectively form
the last point of the inner rectangle to the first point of the outer
rectangle.

To demonstrate this add the following line to the end of your code (right
after e.Graphics.FillPath(pathGrBrush, path); line)

e.Graphics.DrawLines(Pens.Black, path.PathPoints);

Again I assume *p1* is *path*. If it is not change path.PathPoints with
p1.PathPoints

If you want your brush to work correctly. Create a new path for the brush.

GraphicsPath brushPath = new GraphicsPath();
brushPath .AddRectangle(rect1);

You don't need the inner rectangle.

--
HTH
B\rgds
100


Tony Reynolds said:
Hi -

I'm fairly new to C# graphics, but I think I've found a bug in PathGradientBrush.
The following code *should* show a hollowed rectangle. In fact, there is
an indentation on the left edge.
Can someone experienced comment?

Regards

Tony Reynolds

code follows..........

// Set up a GraphicsPath of 2 nested rectangles
RectangleF rect1 = new RectangleF(0,0,300,200);
RectangleF rect2 = new RectangleF(50,50,200,100);

GraphicsPath path = new GraphicsPath();
path.AddRectangle(rect1);
path.AddRectangle(rect2);
path.CloseFigure();

// Set up a PathGradientBrush, use Aqua for start and finish colours to show bug clearly
PathGradientBrush pathGrBrush = new PathGradientBrush(p1);
Color[] colors = {Color.FromArgb(255, 0, 255, 255)};
pathGrBrush.SurroundColors = colors;
pathGrBrush.CenterColor = Color.FromArgb(255, 0, 255, 255);

// Fill the outline
e.Graphics.FillPath(pathGrBrush, path);
 
Stoitcho -

Many thanks for the prompt reply. I still think this is a inconsistency. After all, if you use a plain brush such as
Brush brFill = new SolidBrush(Color.Aqua); in the call to FillPath() you get correct results.

Regards

Tony Reynolds
 
Tony,
With PathGradientBrush you get correct result as well. Just the brush
happens to be with that shape. And it is you who create such a crooked brush
:-)
Use a gradient brush that has rectangular shape and you will have your path
filled as you wish.
The only inconsistentcy I can see is with the name of the brush.

--
B\rgds
100

Tony Reynolds said:
Stoitcho -

Many thanks for the prompt reply. I still think this is a inconsistency.
After all, if you use a plain brush such as
 
Back
Top