c#径向渐变画笔效果在GDI和winforms中

时间:2010-08-19 08:05:49

标签: c# winforms gdi brush

我创建了一个c#windows应用程序并编写了75%的代码。该程序允许用户创建流程图,并根据状态对流程图形状进行着色。我希望它们成为网站Webdesign.org

中的Gel Button等3d按钮

我不是为每个按钮创建一个PNG,而是希望使用画笔或其他技术在C#中创建它们,例如:

// 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 个答案:

答案 0 :(得分:22)

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有许多属性可供试验,以确保您获得所效果。

答案 1 :(得分:11)

看看这个Codeproject article,然后看看Path渐变。