vb.net VB.NET中的渐变填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13992674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gradient fill in VB.NET
提问by dotNET
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):
为什么下面的简单代码会失败?无论我使用 LinearGradientMode 的哪个值,这段代码总是用从左到右的渐变填充路径。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. 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.
如果有人能确认这个问题也发生在他们的机器上,我会很高兴,这样我们就知道这是 GDI+ 的问题,而不是我的代码。仅供参考,我已在装有 .NET Fx 4.0 客户端配置文件和完整版的 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). 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.
首先,感谢所有帮助我发现它的伟大人物。问题是我正在编辑其他人的代码,该代码创建了一个完全命名为 LinearGradientMode 的枚举(以支持他的目的所需的 None 选项)。现在,当他将此枚举的对象发送到 LinearGradientBrush 的构造函数时,C# 编译器会认为最匹配的重载是带“角度”参数的重载(这是我的理论),并将我的渐变模式的值转换为等效的 int (0、1、2、3 和 4 是值)并调用该构造函数,从而在每种情况下产生(几乎)水平梯度。
Thanks again to everyone who participated.
再次感谢所有参与的人。
回答by OneFineDay
Make sure your rectangle has been added to the GraphicsPath.
确保您的矩形已添加到 GraphicsPath。
回答by dotNET
I have worked around the issue (bug?) by manually creating the start and end points for each gradient mode and then using the Two-Points overload of the constructor. Here's the code if someone may be interested:
我通过手动创建每个渐变模式的起点和终点,然后使用构造函数的两点重载来解决这个问题(错误?)。如果有人可能感兴趣,这是代码:
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
I'll wait for more input before marking this as answer.
在将此标记为答案之前,我将等待更多输入。
回答by PurTahan
test this code:
测试这段代码:
Me.CreateGraphics.FillRectangle(New Drawing2D.LinearGradientBrush(Me.ClientRectangle, Color.Blue, Color.Black, Drawing2D.LinearGradientMode.BackwardDiagonal), Me.ClientRectangle)

