c#中GDI+图形图像:GDI+中曲线的填充使用方法|实例
GDI+中曲线的填充
GDI+中,可以使用画笔来填充曲线,Graphics类填充曲线的方法有FillRectangle、FillEllipse、FillPie、FillPolygon、FillClosedCurve、FillPath和FillRegion。只要调用其中的任意一个方法,并使用一种指定的画笔类型作为其参数,就可以填充曲线。
示例
填充曲线
本示例中,当程序运行时,单击【填充曲线】按钮,在窗体中绘制一段图形路径,并使用指定画笔对该图形路径进行填充。示例运行结果如图1所示。

图1 填充曲线
Form1窗体中,单击【填充曲线】按钮,程序首先调用Graphics对象的FillPath方法,将要绘制的图形路径使用指定画笔填充好,然后再调用Graphics对象的DrawPath方法将图形路径绘制出来。【填充曲线】按钮的Click事件代码如下:
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
SolidBrush mySolidBrush = new SolidBrush(Color.GreenYellow);
GraphicsPath myGraphicsPath = new GraphicsPath();
Pen myPen = new Pen(Color.Blue, 1);
Point[] myPoints = { new Point(15, 30), new Point(30, 40), new Point(50, 30) };
myGraphicsPath.AddArc(15, 20, 80, 50, 210, 120);
myGraphicsPath.StartFigure();
myGraphicsPath.AddCurve(myPoints);
myGraphicsPath.AddString("曲线填充", new FontFamily("华文行楷"), (int)FontStyle.Regular, 50, new PointF (20, 50), new StringFormat());
myGraphicsPath.AddPie(180, 20, 80, 50, 210, 120);
graphics.FillPath(mySolidBrush, myGraphicsPath);
graphics.DrawPath(myPen, myGraphicsPath);
}
完整程序代码如下:
★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace _6_13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
SolidBrush mySolidBrush = new SolidBrush(Color.GreenYellow);
GraphicsPath myGraphicsPath = new GraphicsPath();
Pen myPen = new Pen(Color.Blue, 1);
Point[] myPoints = { new Point(15, 30), new Point(30, 40), new Point(50, 30) };
myGraphicsPath.AddArc(15, 20, 80, 50, 210, 120);
myGraphicsPath.StartFigure();
myGraphicsPath.AddCurve(myPoints);
myGraphicsPath.AddString("曲线填充", new FontFamily("华文行楷"), (int)FontStyle.Regular, 50, new PointF(20, 50), new StringFormat());
myGraphicsPath.AddPie(180, 20, 80, 50, 210, 120);
graphics.FillPath(mySolidBrush, myGraphicsPath);
graphics.DrawPath(myPen, myGraphicsPath);
}
}
}
★ ★★★★Form1.Designer.cs窗体设计文件完整程序代码★★★★★
namespace _6_13
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(89, 104);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "填充曲线";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(252, 139);