GDI + .NET: LinearGradientBrush that is greater than 202 pixels wide causes color wrap

If I draw a rectangle wider than 202 pixels wide LinearGradientBrush, I get a color frame on the left:

enter image description here

Given the code for a large rectangle of 202px :

private void MainForm_Paint(object sender, PaintEventArgs e)
{
   Rectangle r = new Rectangle(50, 50, 202, 50);

   Color color1 = Color.FromArgb(unchecked((int)0xFF00024d));
   Color color2 = Color.FromArgb(unchecked((int)0xFFd6a20f));

   Brush b = new LinearGradientBrush(r, color1, color2, LinearGradientMode.Horizontal);
   e.Graphics.FillRectangle(b, r);
}

I get a rectangle that draws correctly:

enter image description here

But if I change the rectangle to 203 pixels wide:

Rectangle r = new Rectangle(50, 50, 203, 50);

The rectangle has a colored frame or wrap on the left:

enter image description here


This also happens in the vertical direction with LinearGradientMode.Vertical:

202px

enter image description here

203px

enter image description here

+5
source share
1 answer

Add this expression before calling FillRectangle ():

 e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;

- .

+10

All Articles