Problem on LinearGradientBrush

  • Thread starter Thread starter Carlo, MCP
  • Start date Start date
C

Carlo, MCP

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)
 
Carlo,

I am not seeing the problem on my laptop. However I see you did
not set the resizeredraw style to the control. It is possible the problem
is caused by the control not completely redrawing itself.

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

Public Sub New()
Me.SetStyle(ControlStyles.ResizeRedraw, True)
End Sub
End Class

Ken
 
Hi Ken

on the original control (where the problem materialized the first time), ResizeRedraw is correctly set to True. I've
post a minimalist version of the original control. However, I also tested this reduced version, and it has the problem
yet.
Please, note that I used a screen magnifier to identify the problem. I believe it's very hard to see on a laptop screen.
Thank you for the help,
Carlo
 
Ok. Reproducing the problem on your IDE means realizing that I not become crazy and this is a good step ahead!

Any ideas?

Carlo
 
Back
Top