Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

vb.net - VB.NET中的渐变填充(Gradient fill in VB.NET)

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.

(无论我使用哪个LinearGradientMode值,此代码始终使用从左到右的渐变填充路径。)

graphPath is a GraphicPath object created elsewhere (basically a rounded rectangle):

(graphPath是在其他位置(基本上是一个圆角矩形)创建的GraphicPath对象:)

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:

(只需创建一个新的VB.NET WinForms项目并将以下代码粘贴到Form1的Paint事件中即可:)

 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.

(如果有人可以确认这个问题也发生在他们的计算机上,我会很高兴,以便我们知道这是GDI +的问题,而不是我的代码。)

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.

(仅供参考,我已经在带有.NET Fx 4.0 Client Profile和完整版的WinXP VM和Win7计算机(32位,Aero模式)上进行了尝试。)

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).

(问题是我正在编辑其他人的代码,这些人创建了一个名为LinearGradientMode的枚举(以支持他为此目的所需的None选项)。)

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.

(现在,当他将这个枚举的对象发送给LinearGradientBrush的构造函数时,C#编译器会认为最接近的匹配重载是采用“ angle”参数的重载(这是我的理论),并将我的渐变模式的值转换为等效的int (0、1、2、3和4是值)并调用该构造函数,在每种情况下都会导致(几乎)水平梯度。)

Thanks again to everyone who participated.

(再次感谢所有参加的人。)

  ask by dotNET translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...