一、使用GDI+画线
.Net中在System.Drawing命名空间下使用Graphics类画线。
有四种方法画线。
//绘制一条连接两个Point结构的线 DrawLine(Pen,Point,Point) //绘制一条连接两个PointF结构的线 DrawLine(Pen,PointF,PointF) //绘制一条连接由坐标对指定的两个点的线条 DrawLine(Pen,Int32,Int32,Int32,Int32) //绘制一条连接由坐标对指定的两个点的线条 DrawLine(Pen,Single,Single,Single,Single)
private void Form1_Paint(object sender, PaintEventArgs e) { //创建画板从Paint事件中的直接引用Graphics对象 Graphics graphics = e.Graphics; //定义画笔 Pen pen = new Pen(Color.Red, 3.0f); //定义两个点 Point pointStart = new Point(100,100); Point pointEnd = new Point(100,500); //画线(线条粗线为3.0f,长度400,垂直于屏幕的一条直线) graphics.DrawLine(pen, pointStart, pointEnd); }
private void Form1_Paint(object sender, PaintEventArgs e) { //创建画板从Paint事件中的直接引用Graphics对象 Graphics graphics = e.Graphics; //定义画笔 Pen pen = new Pen(Color.Red, 3.0f); //定义两个点 PointF pointStart = new PointF(100.0f,100.0f); PointF pointEnd = new PointF(100.0f,500.0f); //画线(线条粗线为3.0f,长度400,垂直于屏幕的一条直线) graphics.DrawLine(pen, pointStart, pointEnd); }
private void Form1_Paint(object sender, PaintEventArgs e) { //创建画板从Paint事件中的直接引用Graphics对象 Graphics graphics = e.Graphics; //定义画笔 Pen pen = new Pen(Color.Red, 3.0f); //定义点坐标 int x1 = 100; int x2 = 100; int y1 = 100; int y2 = 500; //画线(线条粗线为3.0f,长度400,垂直于屏幕的一条直线) graphics.DrawLine(pen, x1,y1,x2,y2); }
private void Form1_Paint(object sender, PaintEventArgs e) { //创建画板从Paint事件中的直接引用Graphics对象 Graphics graphics = e.Graphics; //定义画笔 Pen pen = new Pen(Color.Red, 3.0f); //定义点坐标 int x1 = 100.0f; int x2 = 100.0f; int y1 = 100.0f; int y2 = 500.0f; //画线(线条粗线为3.0f,长度400,垂直于屏幕的一条直线) graphics.DrawLine(pen, x1,y1,x2,y2); }
四种方法,有Point,PointF,Int,Single四种类型,主要是坐标点,画线的时候确定好起始坐标和结束坐标即可。四种中的任何一种都一样。
效果图: