如何绘制圆形渐变?

3
我该如何在vb.net中绘制类似这样的圆形渐变?A Red Circular Gradient
1个回答

5

请查看这个优秀的页面。文章中的代码是用C#编写的。以下是你感兴趣的代码的VB.NET移植版本,并针对矩形填充进行了更新(基于文章中的三角形填充示例):

    Dim pgb As New PathGradientBrush(New Point() { _
        New Point(0, 0), _
        New Point(0, Me.ClientRectangle.Height), _
        New Point(Me.ClientRectangle.Width, Me.ClientRectangle.Height), _
        New Point(Me.ClientRectangle.Width, 0)})
pgb.SurroundColors = New Color() {Color.Red}
pgb.CenterColor = Color.Gray
e.Graphics.FillRectangle(pgb, Me.ClientRectangle)
pgb.Dispose()

这里还有一个可能的解决方案:
Dim pth As New GraphicsPath()
pth.AddEllipse(Me.ClientRectangle)
Dim pgb As New PathGradientBrush(pth)
pgb.SurroundColors = New Color() {Color.Red}
pgb.CenterColor = Color.Gray
e.Graphics.FillRectangle(pgb, Me.ClientRectangle)

请注意,最后的代码片段将绘制一个在矩形内部被限制的圆。如果您希望圆形渐变填充整个矩形,则需要计算一个更大的椭圆路径和更大的矩形。

这是一些很酷的东西,谢谢!最后一段尤其有用。 - Connor Albright
1
一些有用的技巧: 1. 不要让椭圆比矩形更大,保持椭圆的大小不变,在其后面绘制一个外部颜色矩形。2. 内部颜色通常看起来比白色更好,当它是淡化的外部颜色时。 - Connor Albright

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接