6

Why in the world would the following simple code fail? This code always fills the path with the gradient from left to right, no matter which value of LinearGradientMode I use. graphPath is a GraphicPath object created elsewhere (basically a rounded rectangle):

Dim gradBrush as New LinearGradientBrush(rect, color1, color2, LinearGradientMode.Vertical)
graphics.FillPath(gradBrush, graphPath) 

UPDATE

To everyone's wonder, even this fails (draws horizontally). Simply create a new VB.NET WinForms project and paste the following code in Form1's Paint event:

 Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    Dim gradBrush As New LinearGradientBrush(Me.ClientRectangle, Color.Red, Color.White, LinearGradientMode.BackwardDiagonal)
    e.Graphics.FillRectangle(gradBrush, Me.ClientRectangle)
  End Sub

So I don't think this has anything to do with the path construction.

NOTE

I'll be glad if someone could just confirm this issue happens on their machines too, so that we know it is something with GDI+ and not my code. Just for reference, I have tried it on a WinXP VM and Win7 machine (32-bit, Aero mode) with .NET Fx 4.0 Client Profile and Full version.

FINALLY

First of all, thanks to all the great folks who helped me discover it. The problem was that I was editing someone else's code who had created an enum named exactly LinearGradientMode (to support the None option that he needed for his purpose). Now when he sent the object of this enum to LinearGradientBrush's constructor, C# compiler would think that the closest matching overload was the one that take "angle" parameter (this is my theory), and would convert the value of my gradient mode to equivalent int (0, 1, 2, 3 and 4 are the values) and call that constructor, resulting in a (nearly) horizontal gradient in each case.

Thanks again to everyone who participated.

4

3 回答 3

0

确保您的矩形已添加到 GraphicsPath。

于 2012-12-21T15:41:00.270 回答
0

我通过手动为每个渐变模式创建起点和终点,然后使用构造函数的两点重载来解决这个问题(错误?)。如果有人可能感兴趣,这是代码:

  Dim st, en As Point
  Select Case mGradientMode
      Case LinearGradientMode.Vertical
        st = New Point(CInt(rect.X + rect.Width / 2), rect.Y)
        en = New Point(CInt(rect.X + rect.Width / 2), rect.Bottom)
      Case LinearGradientMode.Horizontal
        st = New Point(rect.X, CInt(rect.Y + rect.Height / 2))
        en = New Point(rect.Right, CInt(rect.Y + rect.Height / 2))
      Case LinearGradientMode.ForwardDiagonal
        st = rect.Location
        en = New Point(rect.Right, rect.Bottom)
      Case LinearGradientMode.BackwardDiagonal
        st = New Point(rect.Right, rect.Bottom)
        en = rect.Location
  End Select

  If Me.mGradientMode = LinearGradientMode.None Then
      gradBrush = New LinearGradientBrush(st, en, color1, color1)
  Else
      gradBrush = New LinearGradientBrush(st, en, color1, color2)
  End If

在将此标记为答案之前,我将等待更多输入。

于 2012-12-21T16:21:04.230 回答
0

测试这段代码:

Me.CreateGraphics.FillRectangle(New Drawing2D.LinearGradientBrush(Me.ClientRectangle, Color.Blue, Color.Black, Drawing2D.LinearGradientMode.BackwardDiagonal), Me.ClientRectangle)
于 2018-05-03T23:05:27.220 回答