在GDI和WinForms中实现C#径向渐变刷效果

16
我创建了一个C# Windows应用程序并编写了75%的代码。该程序允许用户创建流程图,并根据其状态着色流程图形状。我希望它们变成3D按钮,例如来自Webdesign.org网站的Gel Button。我想使用刷子或其他技术在C#中创建它们,而不是为每个按钮创建PNG文件。
// Create solid brush.
SolidBrush blueBrush = new SolidBrush(Color.Blue);
// Create points that define polygon.
PointF point1 = new PointF(50.0F, 50.0F);
PointF point2 = new PointF(100.0F, 25.0F);
PointF point3 = new PointF(200.0F, 5.0F);
PointF point4 = new PointF(250.0F, 50.0F);
PointF point5 = new PointF(300.0F, 100.0F);
PointF point6 = new PointF(350.0F, 200.0F);
PointF point7 = new PointF(250.0F, 250.0F);
PointF[] curvePoints = {point1, point2, point3, point4, point5, point6, point7};
// Define fill mode.
FillMode newFillMode = FillMode.Winding;
// Fill polygon to screen.
e.Graphics.FillPolygon(blueBrush, curvePoints, newFillMode);

我知道WPF有径向渐变,但我能在CGI中做类似的事情吗?


2
我猜你是指“GDI+”,而不是“CGI”? - BrainSlugs83
2个回答

25

WPF不同,GDI+/WinForms没有RadialGradientBrush。但是,您可以使用PathGradientBrush来实现相同的效果。

这是一个示例:

Rectangle bounds = ...;
using (var ellipsePath = new GraphicsPath())
{
    ellipsePath.AddEllipse(bounds);
    using (var brush = new PathGradientBrush(ellipsePath))
    {
        brush.CenterPoint = new PointF(bounds.Width/2f, bounds.Height/2f);
        brush.CenterColor = Color.White;
        brush.SurroundColors = new[] { Color.Red };
        brush.FocusScales = new PointF(0, 0);

        e.Graphics.FillRectangle(brush, bounds);
    }
}

PathGradientBrush 有许多属性可供尝试,以确保您获得想要的效果。


3
不同点很大。WF 的 PathGradientBrush 不会在路径之外绘制,而 WPF 的 RadialGradientBrush 会。因此,根据您的要求,可能无法实现相同的效果。 - Gábor

14

2
谢谢,我会尝试一下。 - ClimberM

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