GDI绘图
命名空间:
using System.Drawing.Drawing2D;
绘图基础
GDI概述
Graphics类
是一个析构函数
1.类的实例
需要通过其它类进行辅助创建
Graphics graphics = this.CreateGraphics();
2.类的实例:
需要使用其内部的静态函数创建
绘图准备:
1.准备好目标图片
Bitmap bitmap = new Bitmap(“qp.jpg”);
2.创建绘图对象Graphics
Graphics graphics = this.CreateGraphics();
3.使用目标目标地址创建,在那个对象容器身上使用谁创建对象
4.进行绘图
graphics.DrawImage(bitmap, new Rectangle(0, 0, this.Width, this.Height));
扩展
Paint:在控件需要重新绘制时发生
绘制图形
创建一支画笔
Pen pen = new Pen(Brushes.Red, 2);
绘制弧线
DrawArc
绘制贝塞尔曲线
DrawBezier
graphics.DrawBezier(pen, new Point(100, 100), new Point(200, 200), new Point(300, 100), new Point(400, 200));
绘制实心矩形
FillRectangle
graphics.FillRectangle(Brushes.SkyBlue, 0, 0, 200, 300);
200为宽度,300为高度
绘制直线
DrawLine
graphics.DrawLine(pen, new Point(100, 100), new Point(200, 300));
画笔,起始点和终始点
绘制多边形
FillPolygon
graphics.FillPolygon(Brushes.Tomato, new Point[] { new Point(120, 0), new Point(145, 100), new Point(240, 100), new Point(160, 150), new Point(170, 260), new Point(120, 180), new Point(60, 260), new Point(80, 150), new Point(0, 100), new Point(95, 100) });
画笔,坐标数组(最终是一个实心五角星)
绘制椭圆
DrawEllipse
graphics.DrawEllipse(pen, new Rectangle(new Point(0, 0), new Size(200, 200)));