GDI+是GDI的后继者,它是一种构成 Windows XP 操作系统的子系统的应用程序编程接口。
一般来说有3种基本类型的绘图界面,分别为Windows 窗体上的控件、要发送给打印机的页面和内存中的位图、图像,而Graphics类封装了一个GDI+绘图界面,因此该类提供了可以在3中绘图界面上绘图的功能。
绘制直线
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen pen = new Pen(Color .Blue ,2);
graphics.DrawLine(pen ,50,30,170,30);
}
绘制矩形
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics ();
Pen pen = new Pen(Color .Red ,3);
graphics.DrawRectangle(pen,70,20,80,50);//(x,y)指的是要绘制矩形的左上角的xy坐标
}
绘制椭圆
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen pen = new Pen(Color.Red, 3);
Rectangle myRec = new Rectangle ( 70, 20, 80, 50);//(x,y)指的是要绘制矩形的左上角的xy坐标
graphics.DrawEllipse(pen ,myRec);
}
绘制图形路径
路径是通过组合直线、矩形、简单的曲线而形成的。在GDI+中,GraphicsPath对象允许将基本构造块收集到一个单元中,调用一次Graphics类的DrawPath方法,就可以绘制出整个单元的直线、矩形、多边形和曲线。
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
System.Drawing.Drawing2D.GraphicsPath myGraphicPath = new System.Drawing.Drawing2D.GraphicsPath();
Pen myPen = new Pen(Color .Red ,2);
Point[] myPoints = { new Point(15, 30), new Point(30, 40), new Point(50,30) };
myGraphicPath.AddArc(15,20,80,50,210,120);
myGraphicPath.StartFigure();//不闭合当前图形继续画下一个图形
myGraphicPath.AddCurve(myPoints );
myGraphicPath.AddString("图形路径",new FontFamily ("华文行楷"),(int )FontStyle .Underline ,50,new PointF(20,50),new StringFormat ());
myGraphicPath.AddPie(180,20,80,50,210,120);
graphics.DrawPath(myPen ,myGraphicPath );
}