LinearGradientBrush

G

Guest

I'm trying to draw a linear gradient between two colours using the
LinearGradientBrush class. It sounds simple enough however I get a line of
the second colour at the top of the gradient in certain situations. Placing
the code below in the paint event of a Panel will hopefully demonstrate the
problem:

System.Drawing.Rectangle rect = new Rectangle(0, 10, 100, 60);

using (System.Drawing.Drawing2D.LinearGradientBrush brush = new
System.Drawing.Drawing2D.LinearGradientBrush(rect,
System.Drawing.Color.Yellow, System.Drawing.Color.Red, 90, false))
{
e.Graphics.FillRectangle(brush, rect);
}

The problem only seems to occur when the Y position of the rectangle is
greater than 0, and at random values for the height. The above example works
correctly when the rectangle is (0, 0, 100, 60) or (0, 10, 100, 20). I've
tried this in VS2003 and VS2005 with the same results. Also I've tested this
on five computers and it works on one but the line appears on the others.

Has anyone else seen this problem or have any thoughts on how to avoid it?

Thanks
 
M

Michael Powell

Hi Mike,
whats happening is that your linear gradient is starting just below the top
of your rectangle, and since fills are repetetive, you are getting a line of
the last fill. The following code fixes this problem, however you may like
to talk to someone at microsoft about this, it seems as though their
LinearGradientBrush's rectangle is of by one pixel in certain cases. I have
worked lots with LinearGradientBrushes and never came accross this problem,
so the testers may not have noticed it either.

protected override void OnPaint(PaintEventArgs e)

{

Rectangle rect = new Rectangle(0, 10, 100, 60);

RectangleF brect = new
RectangleF(rect.X,rect.Y-0.1f,rect.Width,rect.Height-0.1f);

LinearGradientBrush lgb = new LinearGradientBrush(brect, Color.Yellow,
Color.Red, 90, false);

e.Graphics.FillRectangle(lgb, rect);

base.OnPaint(e);

}
 
G

Guest

That seems to fix it, many thanks!

Michael Powell said:
Hi Mike,
whats happening is that your linear gradient is starting just below the top
of your rectangle, and since fills are repetetive, you are getting a line of
the last fill. The following code fixes this problem, however you may like
to talk to someone at microsoft about this, it seems as though their
LinearGradientBrush's rectangle is of by one pixel in certain cases. I have
worked lots with LinearGradientBrushes and never came accross this problem,
so the testers may not have noticed it either.

protected override void OnPaint(PaintEventArgs e)

{

Rectangle rect = new Rectangle(0, 10, 100, 60);

RectangleF brect = new
RectangleF(rect.X,rect.Y-0.1f,rect.Width,rect.Height-0.1f);

LinearGradientBrush lgb = new LinearGradientBrush(brect, Color.Yellow,
Color.Red, 90, false);

e.Graphics.FillRectangle(lgb, rect);

base.OnPaint(e);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top