Error on LinearGradientBrush

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello

I found an abnormal behaviour on LinearGradientBrush (or FillRectangle).
This code

Public Class PanelPanel
Inherits System.Windows.Forms.Panel
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
Dim rect As System.Drawing.Rectangle = New
System.Drawing.Rectangle(10, 10, Me.Width - 20, Me.Height - 20)
Dim gradientBrush As System.Drawing.Drawing2D.LinearGradientBrush
gradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(rect, _
System.Drawing.Color.Gray, _

System.Drawing.Color.FloralWhite, _

System.Drawing.Drawing2D.LinearGradientMode.Vertical)
e.Graphics.FillRectangle(gradientBrush, rect)
End Sub
End Class

generates a control with an rectangle drawn inside.
The problem is that an unusual line (colored Color.FloralWhite) appears on
the top edge of the rectangle. This line should not be there.
If LinearGradientMode id other than Vertical the problem persists, but it is
less visible (only a small colored pixel is visible).

Please note that the anomaly appears only with some rectangle sizes. Example:
if rect={X=10,Y=10,Width=304,Height=208} 'equivalent to a Me.Size=New
Size(324,228) - Anomaly visible
if rect={X=10,Y=10,Width=304,Height=216} 'Correct, anomaly not visible

Could someone PLEASE try to reproduce the problem (just copy code as is) and
confirm that is an error and then reason with me??

Thank you!.

Carlo

(MCP)
 
This is a known error (at least known to me) with LinearGradientBrush. It
seems that the colour gradient is calculated slightly too short. The
work-around is to make the gradient 1 pixel larger than the destination
drawing rectangle.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Hello Bob
I'm very glad to meet you on the ng. I recently used your page named "The ToolboxBitmap puzzle"!

Thank you for the suggestion. Applying your suggestion means that I'll get rectangles that overflows my needs by 1
pixel.
Unfortunately, this is unacceptable for my needs since I calculate rectangles sizes based on MeasureString for a very
accurate placement of text(s) and image(s) on the control.
Thank you for replying

Carlo
 
I never heard about WrapMode before. I've seen it is applicable only to
PathGradientBrush.
I've no ideas on how draw my gradient rectangles using PathGradientBrush
(and WrapMode).

Could you please be more precise or write a pair of rows as an example?

This is my original code:

Dim rect As Rectangle = New Rectangle(10, 10, Me.Width - 20, Me.Height - 20)
Dim gradientBrush As Drawing2D.LinearGradientBrush
gradientBrush = New Drawing2D.LinearGradientBrush(rect, Color.Gray,
Color.FloralWhite, Drawing2D.LinearGradientMode.Vertical)
e.Graphics.FillRectangle(gradientBrush, rect)


Thank you, and regards,

Carlo
 
WrapMode works both with LinearGradientBrush and PathGradientBrush. Just set
the WrapMode property on gradientBrush before you draw with it.

Here is the problem: internally GDI+ has a small rounding error when
computing the start and end coordinates of the gradient. This results in 1
pixel of the next gradient showing in your rectangle. Since the default
WrapMode is Tile, the next gradient will begin with a contrasting color, so
that 1 pixel line is obvious. If you change the wrap mode to flip the
gradient, the next gradient will begin with the same color which just ended,
and the error is no longer visible.

Regards,
Frank Hileman

check out VG.net: http://www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio .NET graphics editor
 
Thank you for the reply and for the suggestion. I'm very happy because it
works!

Best Regards,

Carlo
 
Back
Top