C# Graphics类的用法(非常详细)
在 C# 中使用 Graphics 类来绘制几何图形,Graphics 类使用不同的方法实现不同图形的绘制,例如,DrawLine() 方法用于绘制直线,DrawRectangle() 方法用于绘制矩形,DrawEllipse() 方法用于绘制椭圆形等。
Graphics 类中常用的图形绘制方法如下表所示:
【实例】绘制验证码。程序开发步骤如下:
1) 新建一个 Windows 窗体应用程序,在默认窗体 Form1 中添加一个 PictureBox 控件,用来显示图形验证码;添加一个 Button 控件,用来生成图形验证码。
2) 自定义一个 CheckCode 方法,主要使用 Random 类随机生成 4 位验证码,代码如下:
3) 自定义一个 CodeImage 方法,用来将生成的验证码绘制成图片,并显示在 PictureBox 控件中。该方法中有一个 string 类型的参数,用来标识要绘制成图片的验证码。
CodeImage 方法的代码如下:
4) 在窗体的加载事件和 Button 控件的 Click 事件中分别调用 CodeImage 方法绘制验证码,代码如下:

图 1 绘制验证码
【实例】利用饼形图分析产品市场占有率。程序开发步骤如下:
1) 新建一个 Windows 窗体应用程序,在默认窗体 Form1 中添加两个 Panel 控件,分别用来显示绘制的饼形图和说明信息。
2) 首先自定义一个 showPic 方法,主要绘制饼形图,然后在 Form1 窗体的 Paint 事件中获取数据表中的相应数据,并且调用 showPic 方法绘制饼形图。
主要代码如下:

图 2 利用饼形图分析产品市场占有率
【实例】绘制公司Logo。新建一个 Windows 窗体应用程序,触发默认窗体 Form1 的 Paint 事件,在该事件中创建 Graphics 绘图对象,并调用 DrawImage 方法将公司的 Logo 图片绘制到窗体中,代码如下:

图 3 绘制公司 Logo
Graphics 类中常用的图形绘制方法如下表所示:
| 方法 | 说明 | 举例 |
|---|---|---|
| DrawArc() | 弧线 | g.DrawArc(pen, 100, 100, 100, 50, 270, 200); |
| DrawLine() | 直线 |
g.DrawLine(pen, 10, 10, 50, 10); g.DrawLine(pen, 30, 10, 30, 40); |
| DrawEllipse() | 椭圆 | g.DrawEllipse(pen, 10, 10, 50, 30); |
| DrawPie() | 扇形 | g.DrawPie(pen, 100, 100, 50, 30, 270, 200); |
| DrawPolygon() | 多边形 |
Point point1 = new Point(80, 20); Point point2 = new Point(40, 50); Point point3 = new Point(80, 80); Point point4 = new Point(120, 80); Point point5 = new Point(160, 50); Point point6 = new Point(120, 20); Point[] points = {point1, point2, point3, point4, point5, point6}; g.DrawPolygon(pen, points); |
| DrawBezier() | 贝塞尔曲线 | g.DrawBezier(pen, 10, 50, 30, 80, 10, 10, 50, 80); |
| DrawRectangle() | 矩形 | g.DrawRectangle(pen, 10, 10, 100, 50); |
| DrawString() | 文本 | g.DrawString("外星人", new Font("楷体", 16), brush, 10, 10); |
| FillPie() | 实心扇形 | g.FillPie(brush, 100, 100, 50, 30, 270, 200); |
| FillEllipse() | 实心椭圆 | g.FillEllipse(brush, 10, 10, 50, 30); |
| FillPolygon() | 实心多边形 |
Point point1 = new Point(80, 20); Point point2 = new Point(40, 50); Point point3 = new Point(80, 80); Point point4 = new Point(120, 80); Point point5 = new Point(160, 50); Point point6 = new Point(120, 20); Point[] points = {point1, point2, point3, point4, point5, point6}; g.FillPolygon(brush, points); |
| FillRectangle() | 实心矩形 | g.FillRectangle(brush, 10, 10, 100, 50); |
表中第 3 列的 g 表示 Graphics 对象。
C# Graphics绘制图形
Graphics 类中绘制几何图形的方法都是以 Draw 开头的,下面通过一个具体的实例演示如何绘制图形。【实例】绘制验证码。程序开发步骤如下:
1) 新建一个 Windows 窗体应用程序,在默认窗体 Form1 中添加一个 PictureBox 控件,用来显示图形验证码;添加一个 Button 控件,用来生成图形验证码。
2) 自定义一个 CheckCode 方法,主要使用 Random 类随机生成 4 位验证码,代码如下:
private string CheckCode() // 此方法生成验证码
{
int number;
char code;
string checkCode = String.Empty; // 存储随机生成的 4 位英文或数字
Random random = new Random(); // 生成随机数
for (int i = 0; i < 4; i++)
{
number = random.Next(); // 返回非负随机数
if (number % 2 == 0) // 判断数字是否为偶数
code = (char)('0' + (char)(number % 10));
else // 如果不是偶数
code = (char)('A' + (char)(number % 26));
checkCode += " " + code.ToString(); // 累加字符串
}
return checkCode; // 返回生成的字符串
}
3) 自定义一个 CodeImage 方法,用来将生成的验证码绘制成图片,并显示在 PictureBox 控件中。该方法中有一个 string 类型的参数,用来标识要绘制成图片的验证码。
CodeImage 方法的代码如下:
private void CodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 9.5)), 22);
Graphics g = Graphics.FromImage(image); // 创建 Graphics 对象
try
{
Random random = new Random(); // 生成随机数
g.Clear(Color.White); // 清空图片背景色
for (int i = 0; i < 3; i++) // 画背景噪声线
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, FontStyle.Bold);
g.DrawString(checkCode, font, new SolidBrush(Color.Red), 2, 2);
for (int i = 0; i < 150; i++) // 画前景噪声点
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
// 画边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
this.pictureBox1.Width = image.Width; // 设置 PictureBox 的宽度
this.pictureBox1.Height = image.Height; // 设置 PictureBox 的高度
this.pictureBox1.BackgroundImage = image; // 设置 PictureBox 的背景图像
}
catch { }
}
4) 在窗体的加载事件和 Button 控件的 Click 事件中分别调用 CodeImage 方法绘制验证码,代码如下:
private void Form1_Load(object sender, EventArgs e)
{
CodeImage(CheckCode());
}
private void button1_Click(object sender, EventArgs e)
{
CodeImage(CheckCode());
}
程序运行结果如下图所示:
图 1 绘制验证码
C# Graphics填充图形
Graphics 类中填充几何图形的方法都是以 Fill 开头的,下面通过一个具体的实例演示以 Fill 开头的方法在实际开发中的应用。【实例】利用饼形图分析产品市场占有率。程序开发步骤如下:
1) 新建一个 Windows 窗体应用程序,在默认窗体 Form1 中添加两个 Panel 控件,分别用来显示绘制的饼形图和说明信息。
2) 首先自定义一个 showPic 方法,主要绘制饼形图,然后在 Form1 窗体的 Paint 事件中获取数据表中的相应数据,并且调用 showPic 方法绘制饼形图。
主要代码如下:
private void showPic(float f, Brush B)
{
Graphics g = this.panel1.CreateGraphics(); // 通过 panel1 创建 Graphics
if (TimeNum == 0.0f)
g.FillPie(B, 0, 0, this.panel1.Width, this.panel1.Height, 0, f * 360);
else
g.FillPie(B, 0, 0, this.panel1.Width, this.panel1.Height, TimeNum, f * 360);
TimeNum += f * 360;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
ht.Clear();
Conn(); // 连接数据库
Random rnd = new Random();
using (cmd = new SqlCommand(
"select t_Name,sum(t_Num) as Num from tb_product group by t_Name", con))
{
Graphics g2 = this.panel2.CreateGraphics();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) // 读取数据
{
ht.Add(dr[0], Convert.ToInt32(dr[1])); // 加入 Hashtable
}
float[] flo = new float[ht.Count];
int T = 0;
foreach (System.Collections.DictionaryEntry de in ht) // 遍历 Hashtable
{
flo[T] = Convert.ToSingle(
(Convert.ToDouble(de.Value) / SumNum).ToString().Substring(0, 6));
Brush Bru = new SolidBrush(
Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
// 绘制文字百分比
g2.DrawString(
de.Key + " " + (flo[T] * 100).ToString() + "%",
new Font("Arial", 8, FontStyle.Regular),
Bru, 7, 5 + T * 18);
showPic(flo[T], Bru); // 绘制饼图扇形
T++;
}
}
}
程序运行结果如下图所示:
图 2 利用饼形图分析产品市场占有率
C# Graphics绘制图像
Graphics 绘图类不仅可以绘制几何图形和文本,还可以绘制图像,绘制图像时需要使用 DrawImage 方法,该方法可以在由一对坐标指定的位置以图像的原始大小或者指定大小绘制图像,它有多种使用形式,其常用的语法格式如下:public void DrawImage(Image image, int x, int y); public void DrawImage(Image image, int x, int y, int width, int height);参数及说明如下表所示:
| 参数 | 说明 |
|---|---|
| image | 要绘制的 Image |
| x | 所绘制图像的左上角的 x 坐标 |
| y | 所绘制图像的左上角的 y 坐标 |
| width | 所绘制图像的宽度 |
| height | 所绘制图像的高度 |
【实例】绘制公司Logo。新建一个 Windows 窗体应用程序,触发默认窗体 Form1 的 Paint 事件,在该事件中创建 Graphics 绘图对象,并调用 DrawImage 方法将公司的 Logo 图片绘制到窗体中,代码如下:
private void Form1_Paint(object sender, PaintEventArgs e)
{
// 创建 Image 对象
Image myImage = Image.FromFile("logo.jpg");
Graphics myGraphics = this.CreateGraphics(); // 创建 Graphics 对象
// 绘制图像
myGraphics.DrawImage(myImage, 50, 20, 90, 92);
}
logo.jpg 文件需要存放到项目的 Debug 文件夹中。程序运行结果如下图所示:
图 3 绘制公司 Logo
ICP备案:
公安联网备案: