Advertisement

04.16.2008 at 03:16PM PDT, ID: 23329115
[x]
Attachment Details

Artifact created with LinearGradientBrush and FillRectangle

Asked by LastUntakenName in Microsoft Visual C#.Net, Algorithms, Visual Studio .NET 2005

Tags: C#

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.Start Free Trial
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();
        }
 
Loading Advertisement...
 
[+][-]04.17.2008 at 12:16AM PDT, ID: 21374829

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.17.2008 at 09:23AM PDT, ID: 21378802

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.17.2008 at 10:18PM PDT, ID: 21383454

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.18.2008 at 03:21PM PDT, ID: 21390077

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.18.2008 at 03:30PM PDT, ID: 21390116

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.14.2008 at 08:31PM PDT, ID: 21570500

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual C#.Net, Algorithms, Visual Studio .NET 2005
Tags: C#
Sign Up Now!
Solution Provided By: LastUntakenName
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628