Problem with LinearGradientBrush
-
I was testing the LinearGradientBrush functionality and came across a behavior that is totally unexpected and I hope someone can help me out. I am overriding the
OnPaint()
method of my form and all I am trying to do is draw a linear gradient from blue to yellow inside a rectangle, here is the code:protected override void OnPaint(PaintEventArgs e) { base.OnPaint (e); Rectangle rc = new Rectangle(10,10,80,15 + this.mHeightOffset); Point p1 = new Point(rc.X, rc.Y); Point p2 = new Point(rc.X, rc.Y + rc.Height); LinearGradientBrush br = new LinearGradientBrush(p1, p2, Color.Blue, Color.Yellow); e.Graphics.FillRectangle(br, rc); }
The problem is that the gradient shows the first row of pixels as yellow instead of blue. That's why I added the variable mHeightOffset and verified that the linear gradient doesn't behave as expected for all heights of the rectangle. Does anyone have any ideas why this would happen? :confused: -
I was testing the LinearGradientBrush functionality and came across a behavior that is totally unexpected and I hope someone can help me out. I am overriding the
OnPaint()
method of my form and all I am trying to do is draw a linear gradient from blue to yellow inside a rectangle, here is the code:protected override void OnPaint(PaintEventArgs e) { base.OnPaint (e); Rectangle rc = new Rectangle(10,10,80,15 + this.mHeightOffset); Point p1 = new Point(rc.X, rc.Y); Point p2 = new Point(rc.X, rc.Y + rc.Height); LinearGradientBrush br = new LinearGradientBrush(p1, p2, Color.Blue, Color.Yellow); e.Graphics.FillRectangle(br, rc); }
The problem is that the gradient shows the first row of pixels as yellow instead of blue. That's why I added the variable mHeightOffset and verified that the linear gradient doesn't behave as expected for all heights of the rectangle. Does anyone have any ideas why this would happen? :confused:Well... Ive tested the code snippet and it looks like it should: A clear gradient from blue to yellow.
-
Well... Ive tested the code snippet and it looks like it should: A clear gradient from blue to yellow.
Robert, Thanks for testing it, that's weird that it works fine on your computer and not on mine. Maybe it has to do with the display resolutions. That's why I had a variable called "mHeightOffset" which I kept changing and noticed that increasing the height just by 1 pixel got rid of the yellow line; however, if I kept increasing then the line came back. I guess this behavior is expected if the rectangle to be filled is larger than the distance between the points specified in LinearGradientBrush; however, I am using the exact same distance. Please let me know if the code still works fine as you change mHeightOffset. To do it you can add a NumericUpDown control and call Invalidate() every time the value is changed. Thanks, Rudy.
-
I was testing the LinearGradientBrush functionality and came across a behavior that is totally unexpected and I hope someone can help me out. I am overriding the
OnPaint()
method of my form and all I am trying to do is draw a linear gradient from blue to yellow inside a rectangle, here is the code:protected override void OnPaint(PaintEventArgs e) { base.OnPaint (e); Rectangle rc = new Rectangle(10,10,80,15 + this.mHeightOffset); Point p1 = new Point(rc.X, rc.Y); Point p2 = new Point(rc.X, rc.Y + rc.Height); LinearGradientBrush br = new LinearGradientBrush(p1, p2, Color.Blue, Color.Yellow); e.Graphics.FillRectangle(br, rc); }
The problem is that the gradient shows the first row of pixels as yellow instead of blue. That's why I added the variable mHeightOffset and verified that the linear gradient doesn't behave as expected for all heights of the rectangle. Does anyone have any ideas why this would happen? :confused:I suspect you're getting an off by one error, because if the top left corner is 0,0, say, and your rectangle is 20 pixels high, then your bottom corner will be 0, 20. However, a 20 pixel high rectangle would end at 0, 19. Try Height - 1. Christian Graus - Microsoft MVP - C++
-
Robert, Thanks for testing it, that's weird that it works fine on your computer and not on mine. Maybe it has to do with the display resolutions. That's why I had a variable called "mHeightOffset" which I kept changing and noticed that increasing the height just by 1 pixel got rid of the yellow line; however, if I kept increasing then the line came back. I guess this behavior is expected if the rectangle to be filled is larger than the distance between the points specified in LinearGradientBrush; however, I am using the exact same distance. Please let me know if the code still works fine as you change mHeightOffset. To do it you can add a NumericUpDown control and call Invalidate() every time the value is changed. Thanks, Rudy.
Im astonished. You are totally right. I work frequently with the LinearGradientBrush but that never happened to me... You only workaroundf for this is probably changing the first point of the brush by one pixel:
Point p1 = new Point(rc.X, rc.Y-1);
-
I suspect you're getting an off by one error, because if the top left corner is 0,0, say, and your rectangle is 20 pixels high, then your bottom corner will be 0, 20. However, a 20 pixel high rectangle would end at 0, 19. Try Height - 1. Christian Graus - Microsoft MVP - C++
Thanks Christian, I tried something similar and the problem gets fixed for one height and not for all heights. What I did was place a couple of controls on my form, one to adjust the drawing height and another to adjust the height of my LinearGradientBrush. Usually setting the height of the LinearGradientBrush smaller shows the error mentioned; however, setting it larger by one pixel fixes the problem for some heights but not for all so the fix is not consistent. I gave up on this problem and placed a border around my drawing to hide the incorrect row of pixels. Thanks, Rudy.
-
I suspect you're getting an off by one error, because if the top left corner is 0,0, say, and your rectangle is 20 pixels high, then your bottom corner will be 0, 20. However, a 20 pixel high rectangle would end at 0, 19. Try Height - 1. Christian Graus - Microsoft MVP - C++
I figured out how to get it working, although it's not logical. The solution is to set the starting position of the LinearGradientBrush 1 pixel before the rectangle to be drawn. Originally in my code I had:
Rectangle rc = new Rectangle(10,10,80,15 + this.mHeightOffset); Point p1 = new Point(rc.X, rc.Y); Point p2 = new Point(rc.X, rc.Y + rc.Height);
so I changedPoint p1 = new Point(rc.X, rc.Y)
to:Point p1 = new Point(rc.X, rc.Y **- 1**)
Everything else stayed the same and I removed variablemHeightOffset
-
Im astonished. You are totally right. I work frequently with the LinearGradientBrush but that never happened to me... You only workaroundf for this is probably changing the first point of the brush by one pixel:
Point p1 = new Point(rc.X, rc.Y-1);