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

Re: LinearGradientBrush by Michael

Michael
Tue Jun 13 07:24:28 CDT 2006

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);

}


--
Mike Powell
Ramuseco Limited
www.ramuseco.com


"Mike Batt" <MikeBatt@discussions.microsoft.com> wrote in message
news:7027BF4C-D07F-401D-84F2-96BB72860327@microsoft.com...
> 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
>



Re: LinearGradientBrush by MikeBatt

MikeBatt
Tue Jun 13 08:39:02 CDT 2006

That seems to fix it, many thanks!

"Michael Powell" wrote:

> 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);
>
> }
>
>
> --
> Mike Powell
> Ramuseco Limited
> www.ramuseco.com
>
>
> "Mike Batt" <MikeBatt@discussions.microsoft.com> wrote in message
> news:7027BF4C-D07F-401D-84F2-96BB72860327@microsoft.com...
> > 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
> >
>
>
>