Graphics Class

Posted on 2019-04-09 10:13  金色的省略号  阅读(134)  评论(0编辑  收藏  举报

System.Drawing 封装一个 GDI+ 绘图图面。 此类不能被继承。

https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.graphics?view=netframework-4.7.2#%E6%96%B9%E6%B3%95

 

 1 using System;
 2 using System.Drawing;
 3 using System.Collections;
 4 using System.ComponentModel;
 5 using System.Windows.Forms;
 6 using System.Data;
 7   
 8 public class Form1 : Form
 9 {
10     public Form1()
11     {
12         this.AutoScaleBaseSize = new Size(6, 14);
13         this.ClientSize = new Size(400, 400);
14         this.Paint += new PaintEventHandler(this.Form1_Paint);
15         this.Click += new EventHandler( this.Draw );
16     }
17     static void Main() 
18     {
19         Application.Run(new Form1());
20     }
21     
22     //Paint事件调用
23     private void Form1_Paint(object sender, PaintEventArgs e)
24     {
25         Graphics g = e.Graphics;//传进来的对象不需要释放
26         
27         Brush brush = new SolidBrush(Color.Green);
28         Font font = new Font("宋体", 25);
29                 
30         g.DrawString("绘制", font, brush, 250, 20);        
31     }
32     
33     //在已存在的窗体上创建Graphics对象
34     //窗体控件,Click事件
35     private void Draw(object sender, EventArgs e)
36     {   
37         Graphics g = this.CreateGraphics();
38         
39         Pen pen = new Pen(Color.Red, 2);
40         Brush brush = new SolidBrush(Color.Blue);
41         Font font = new Font("宋体", 25);
42         Rectangle rect = new Rectangle(20, 120, 100, 160);
43         
44         g.DrawLine(pen, 20, 100, 100, 100);
45         g.DrawRectangle(pen, rect);
46         g.DrawString("GDI+图形编程", font, brush, 20, 20);
47         
48         brush.Dispose(); font.Dispose(); pen.Dispose();//释放对象
49         g.Dispose();
50     }   
51 }
Graphics类方法的使用