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

各グラデーション モードの開始点と終了点を手動で作成し、コンストラクターの Two-Points オーバーロードを使用して、この問題 (バグ?) を回避しました。誰かが興味を持っているかもしれない場合のコードは次のとおりです。

  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 に答える