Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)qqpublished in(发表于) 2014/7/11 9:16:34 c#中GDI+图形图像:GDI+中的图形路径使用方法
c#中GDI+图形图像:GDI+中的图形路径使用方法|实例
GDI+中的图形路径路径是通过组合直线、矩形和简单的曲线而形成的。在GDI+中,GraphicsPath对象允许将基本构造块收集到一个单元中,调用一次Graphics类的DrawPath方法,就可以绘制出整个单元的直线、矩形、多边形和曲线。GraphicsPath类提供了下列方法来创建要绘制的图形路径:AddLine、AddRectangle、AddEllipse、AddArc、AddPolygon、AddCurve(针对基数样条)和AddBezier。这些方法中的任何一种都是可重载的,即每种方法都支持几个不同的参数列表。例如,AddLine方法的一种格式接收4个整数,而其另一种格式则接收两个Point对象。将直线、矩形和贝塞尔样条添加到路径的方法具有多个伴随方法,这些伴随方法在一次调用中将多个项添加到路径。伴随方法包括AddLines、AddRectangles和AddBeziers。同样,AddCurve和AddArc方法也有将闭合曲线或扇形添加到路径的伴随方法。例如,AddClosedCurve和AddPie。若要绘制路径,需要Graphics对象、Pen对象和GraphicsPath对象。Graphics对象提供DrawPath方法,Pen对象存储用于呈现路径的线条属性,如宽度和颜色,而GraphicsPath对象存储构成路径的直线和曲线序列。Pen对象和GraphicsPath对象作为参数传递给DrawPath方法。DrawPath方法用来绘制GraphicsPath图形路径,其语法格式如下:public void DrawPath (Pen pen,GraphicsPath path)参数说明如下。pen:Pen对象,它确定路径的颜色、宽度和样式。path:要绘制的GraphicsPath图形路径。示例绘制图形路径本示例中,当程序运行时,单击【绘制图形路径】按钮,在窗体中根据条件绘制指定的图形路径。示例运行结果如图1所示。图1 绘制图形路径Form1窗体中,在【绘制图形路径】按钮的Click事件中分别声明Graphics类、GraphicsPath类和Pen类的3个实例对象,然后声明一个Point结构数组,用来定义基数样条的各顶点,同时调用GraphicsPath对象的AddArc方法、AddCurve方法、AddString方法和AddPie方法,增加图形路径,最后调用Graphics对象的DrawPath方法在窗体中绘制指定的图形路径。【绘制图形路径】按钮的Click事件代码如下:private void button1_Click(object sender, EventArgs e){Graphics graphics = this.CreateGraphics();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.Underline,50,new PointF(20,50),new StringFormat());myGraphicsPath.AddPie(180, 20, 80, 50, 210, 120);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_08{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){Graphics graphics = this.CreateGraphics();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.Underline, 50, new PointF(20, 50), new StringFormat());myGraphicsPath.AddPie(180, 20, 80, 50, 210, 120);graphics.DrawPath(myPen, myGraphicsPath);}}}★ ★★★★Form1.Designer.cs窗体设计文件完整程序代码★★★★★namespace _6_08{partial class Form1{/// /// 必需的设计器变量。/// private System.ComponentModel.IContainer components = null;/// /// 清理所有正在使用的资源。 赞 If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长) QQ:154298438 QQ:417480759
赞