1.简介
Graphics是System.Drawing里面的绘图类。
2.开始绘制
2.1.创建绘图对象,三种方法
- 在窗体或控件的Paint事件中,用PaintEventArgs创创建绘图对象(控件或窗体重绘时)
/// <summary>
/// 1.在窗体或控件的Paint事件中,用PaintEventArgs创创建绘图对象(控件或窗体重绘时)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMain_Paint(object sender, PaintEventArgs e)
{
Graphics g1 = e.Graphics;//绘图对象
}
- 在panel中绘图
private void button_line_Click(object sender, EventArgs e)
{
//2.创建绘图对象,在panel中绘图
Graphics g2 = panel1.CreateGraphics();
}
- 基于图片绘制
private void button_line_Click(object sender, EventArgs e)
{
//3/基于图片绘制
//Graphics g3 = Graphics.FromImage(Image.FromFile("images/picture1.jpg"));
//设置呈现质量
g2.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿
}
2.2.设置呈现质量
private void button_line_Click(object sender, EventArgs e)
{
//2.创建绘图对象,在panel中绘图
Graphics g2 = panel1.CreateGraphics();
//3/基于图片绘制
//Graphics g3 = Graphics.FromImage(Image.FromFile("images/picture1.jpg"));
//设置呈现质量
g2.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿
}
2.3.Graphics的方法
2.4.形状绘制与填充
2.4.1.画直线
实例:
private void button_line_Click(object sender, EventArgs e)
{
//2.创建绘图对象,在panel中绘图
Graphics g2 = panel1.CreateGraphics();
//3/基于图片绘制
//Graphics g3 = Graphics.FromImage(Image.FromFile("images/picture1.jpg"));
//设置呈现质量
g2.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿
Pen pen01 = new Pen(Color.Black,1);
g2.DrawLine(pen01, 10, 10, 40, 10);//画一条直线,起点(10,10),终点(40,10)
g2.DrawLine(pen01, new Point(40, 10), new Point(40, 60));
pen01.Dispose();//释放对象
g2.Dispose();
}