Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c# radial gradient brush effect in GDI and winforms

I have created a c# windows application and written 75% of the code. The program allows the user to create a flow chart, and will shade the flow chart shapes according to their status. I wanted them to become 3d buttons such as Gel Button from the website Webdesign.org

Rather than create a PNG for each button, I wanted to create them in C# using brushes or other technique, such as:

// 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);

I know WPF has radial gradients, but can I do something simular in CGI?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Unlike WPF, GDI+/WinForms does not have a RadialGradientBrush. However you can achieve the same effect using a PathGradientBrush.

Here's an example:

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);
    }
}

The PathGradientBrush has lots of properties to experiment with to ensure you get the effect you're after.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...