Question : Artifact created with LinearGradientBrush and FillRectangle

I am trying to create a symmetric gradient background for a panel - or any other paintable object. The following code works for the most part. However, as I change the form size, occasionally a line of the other-end color appears in the middle. This artifact appears consistently at the same form heights. The actual artifact is part of the bitmap. I know because I saved the bitmap several times, all coinciding with the form's artifact or lack thereof. Any help would be appreciated.

The form simply has a panel (panel1) docked to the right.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
private void GradientFill(Control paintedObject, Color firstColor, Color secondColor, LinearGradientMode linearGradientMode, bool symmetric) {
            int width = paintedObject.Width;
            int height = paintedObject.Height;
            Bitmap bmp = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bmp);
            if (symmetric) {
                int widthHalf = width / 2;
                int heightHalf = height / 2;
                Rectangle firstBrushRectangle;
                Rectangle firstFillRectangle;
                Rectangle secondBrushRectangle;
                Rectangle secondFillRectangle;
                if (linearGradientMode == LinearGradientMode.Vertical) {
                    firstBrushRectangle = new Rectangle(0, 0, width, heightHalf);
                    firstFillRectangle = new Rectangle(0, 0, width, heightHalf);
                    secondBrushRectangle = new Rectangle(0, heightHalf, width, height - heightHalf);
                    secondFillRectangle = new Rectangle(0, heightHalf, width, height - heightHalf);
                } else {
                    firstBrushRectangle = new Rectangle(0, 0, widthHalf, height);
                    firstFillRectangle = new Rectangle(0, 0, widthHalf, height);
                    secondBrushRectangle = new Rectangle(widthHalf, 0, width - widthHalf, height);
                    secondFillRectangle = new Rectangle(widthHalf, 0, width - widthHalf, height);
                }
                LinearGradientBrush firstGradientBrush = new LinearGradientBrush(firstBrushRectangle, firstColor, secondColor, linearGradientMode);
                LinearGradientBrush secondGradientBrush = new LinearGradientBrush(secondBrushRectangle, secondColor, firstColor, linearGradientMode);
                g.FillRectangle(firstGradientBrush, firstFillRectangle);
                g.FillRectangle(secondGradientBrush, secondFillRectangle);
            } else {
                Rectangle baseRectangle = new Rectangle(0, 0, width, height);
                LinearGradientBrush gradientBrush = new LinearGradientBrush(baseRectangle, firstColor, secondColor, linearGradientMode);
                g.FillRectangle(gradientBrush, baseRectangle);
            }
            paintedObject.BackgroundImage = bmp;
        }
 
        private void panel1_Resize(object sender, EventArgs e) {
            GradientFill((Control)sender, Color.BlueViolet, Color.LimeGreen, LinearGradientMode.Vertical, true);
            textBox1.Text = panel1.Height.ToString();
        }

Answer : Artifact created with LinearGradientBrush and FillRectangle

I never found out what was causing the artifact. However, I found a solution to achieve the same goal. I am using the colorBlend and positions properties instead.
Random Solutions  
 
programming4us programming4us