最近需要使用到C#DrawLine绘制直线这个功能,对这个了解的不多,记录一下使用的时候遇到的问题。
绘制线的基础部分,这个之前在《C#自学笔记(四十)之Windows绘图》就写过,有兴趣的可以看下
我这里主要说下Graphics类。
GDI+是GDI的后继者,它是.NET Framework为操作图形提供的应用程序编程接口,主要用在窗体上绘制各种图形图像,可以用于绘制各种数据图像、数学仿真等。
Graphics类是GDI+的核心,它提供将对象绘制到显式设备的方法。Graphics类封装了绘制直线、曲线、圆形、图像和文本的方法,是一切GDI+操作的基础类。在绘图之前,必须在指定的窗体上创建一个Graphics对象,才能调用Graphics类的方法画图。
1.1 Paint事件
在窗体或控件的Paint事件中创建,将其作为PaintEventArgs的一部分。在为控件创建绘制代码时,通常会使用此方法。
例如,在Paint事件中创建Graphics对象:
private void pictureBox1_Paint(object sender, PaintEventArgs e) { //创建Graphics对象实例 Graphics g = e.Graphics; //设置画笔颜色和宽度 Pen p = new Pen(Color.Black, 1); // 绘制直线 g.DrawLine(p, x1, y1, x2, y2); }
1.2 CreateGraphics方法
调用窗体或控件的CreateGraphics方法可以获取对Graphics对象的引用,该对象表示控件或窗体的绘图画面。如果在已存在的窗体或控件身上绘图,应该使用此方法
例如,在窗体的Load事件中,通过CreateGraphics方法创建Graphics对象
private void Form1_MouseUp(object sender, MouseEventArgs e) { rightPointX = e.X; rightPointY = e.Y; //创建Graphics对象实例 Graphics g = this.CreateGraphics(); //设置画笔颜色和宽度 Pen p = new Pen(Color.Black, 1); // 绘制直线 g.DrawLine(p, leftPointX, leftPointY, rightPointX, rightPointY); }
1.3 Graphics.FromImage方法
由从Image继承的任何对象创建Graphics对象,调用Graphics.FromImage方法即可,该方法在需要更改已存在的图像时十分有用,例:
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { rightPointX = e.X; rightPointY = e.Y; //创建Graphics对象实例 //Graphics g = this.CreateGraphics(); g = Graphics.FromImage(myImage); //设置画笔颜色和宽度 Pen p = new Pen(Color.Black, 1); // 绘制直线 g.DrawLine(p, leftPointX, leftPointY, rightPointX, rightPointY); pictureBox1.Refresh(); }
下边展示了三个例子:只是展示部分代码,资源在文末,可下载。
C#在窗体上绘制直线。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DrawLine { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } /// <summary> /// 左上角点坐标 /// </summary> public int leftPointX = 0; public int leftPointY = 0; /// <summary> /// 右上角点坐标 /// </summary> public int rightPointX = 0; public int rightPointY = 0; private void Form1_MouseDown(object sender, MouseEventArgs e) { leftPointX = e.X; leftPointY = e.Y; } private void Form1_MouseUp(object sender, MouseEventArgs e) { rightPointX = e.X; rightPointY = e.Y; //创建Graphics对象实例 Graphics g = this.CreateGraphics(); //设置画笔颜色和宽度 Pen p = new Pen(Color.Black, 1); // 绘制直线 g.DrawLine(p, leftPointX, leftPointY, rightPointX, rightPointY); } } }
C#在picturebox上绘制直线
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DrawLine_pictureBox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } public int x1; public int y1; public int x2; public int y2; private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { x1 = e.X; y1 = e.Y; } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { x2 = e.X; y2 = e.Y; pictureBox1.Refresh(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { //创建Graphics对象实例 Graphics g = e.Graphics; //设置画笔颜色和宽度 Pen p = new Pen(Color.Black, 1); // 绘制直线 g.DrawLine(p, x1, y1, x2, y2); } } }
C#在bitmap上绘制直线。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DrawLine { public partial class Form1 : Form { Graphics g; Bitmap myImage; int PBwidth, PBheight; public Form1() { InitializeComponent(); PBwidth = pictureBox1.Width; PBheight = pictureBox1.Height; myImage = new Bitmap(PBwidth, PBwidth); g = Graphics.FromImage(myImage); //g.Clear(Color.White); pictureBox1.Image = myImage; } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { leftPointX = e.X; leftPointY = e.Y; } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { rightPointX = e.X; rightPointY = e.Y; //创建Graphics对象实例 //Graphics g = this.CreateGraphics(); //设置画笔颜色和宽度 Pen p = new Pen(Color.Black, 1); // 绘制直线 g.DrawLine(p, leftPointX, leftPointY, rightPointX, rightPointY); pictureBox1.Refresh(); } /// <summary> /// 左上角点坐标 /// </summary> public int leftPointX = 0; public int leftPointY = 0; /// <summary> /// 右上角点坐标 /// </summary> public int rightPointX = 0; public int rightPointY = 0; } }
可以注意一下上边三个示例中对Graphics的调用。
具体Graphics的属性及其调用的方法请点击msdn查看