1.Graphics

  Graphics对象是GDI+绘图表面,因此在Windows窗体应用程序中要使用GDI+创建绘图,必须要先创建Graphics.在给窗体注册一个Paint事件后,Graphics的创建方式有以下三种。

1)直接获取Paint事件的PaintEvenlArgs中Graphics对象(最常用)。

1 private void Form1_Paint(object sender, PaintEventArgs e)
2 {
3     Graphics g1 = e.Graphics;
4 }    

2)从Image中创建Graphics对象。

1         private void Form1_Paint(object sender, PaintEventArgs e)
2         {
3             //创建一个Image对象
4             Bitmap imgTemp = new Bitmap(200, 200);
5             Graphics g2 = Graphics.FromImage(imgTemp);
6         }    

3)用CreateGraphics方法创建Graphics对象。

1         private void Form1_Paint(object sender, PaintEventArgs e)
2         {
3             Graphics g3 = this.CreateGraphics();
4         }

2.Pen

  在Graphics这张图纸准备好了以后,我们就可以使用Pen(钢笔)进行绘图了。Pen类位于System.Drawing名称空间中(在Windows窗体应用程序当中不需要单独引用)。最简单的创建方式为:

Pen p = new Pen(Color.White,1);        //创建颜色为白色,像素为1的钢笔

  下面为Pen的一个应用实例

1 private void Form1_Paint(object sender, PaintEventArgs e)
2 {
3     Graphics g1 = e.Graphics;
4 
5     Pen p = new Pen(Color.Blue, 2);     //定义笔颜色蓝色,大小2个像素
6     g1.DrawEllipse(p, 0, 80, 60, 30);   //绘制一个椭圆,椭圆的外切矩形左上坐标为(0,80),矩形长(x)宽(y)为(60,30)
7     p.Dispose();        //释放Pen所占用的资源
8     g1.Dispose();       //释放由Graphics使用的资源
9 }

  绘图结果如下

Pen应用示例

3.Brush

  Brush(笔刷)和Pen类似,不过Brush类本身是一个抽象类不能直接实例化。GDI+的API提供五个类,扩展并提供了Brush类的具体实现。这5个类分别是:

1)SolidBrush  使用纯颜色填充图形 

应用实例

 1 private void Form1_Paint(object sender, PaintEventArgs e)
 2 {
 3     Graphics g = e.Graphics;
 4     
 5     //SolidBrush的使用: 使用纯颜色填充图形
 6     Brush bh = new SolidBrush(Color.Black);
 7     g.FillEllipse(bh, 0, 80, 60, 30);       //绘制一个椭圆
 8     bh.Dispose();   //释放资源
 9     g.Dispose();
10 }

绘图结果:

2)TextureBrush  使用基于光栅的图像(位图,JPG等图像)填充图形

应用实例:

  首先找一种要填充的图片,最好在项目中创建一个img文件夹,将其添加到文件夹中

 1 private void Form1_Paint(object sender, PaintEventArgs e)
 2 {
 3     Graphics g = e.Graphics;
 4 
 5     //TextureBrush的使用:使用基于光栅的图像(位图,JPG等图像)填充图形
 6     string path = @"E:\软件\Microsoft Visual Studio 10.0\project\CSharp\22GDI+图形图像处理技术\22_3创建Brush对象\22_3创建Brush对象\imgs\1.png";
 7     Bitmap img;
 8     if (File.Exists(path))
 9     {
10         img = new Bitmap(path);
11         Brush br = new TextureBrush(img);
12         g.FillEllipse(br, 0, 80, 650, 280);
13         br.Dispose();
14     }
15     else
16         MessageBox.Show("要填充的图片不存在!");
17     g.Dispose();
18 }

绘图结果

3)LinearGradientBrush  使用颜色渐变填充图形

应用实例

 1 private void Form1_Paint(object sender, PaintEventArgs e)
 2 {
 3     Graphics g = e.Graphics;
 4     
 5     //LinearGradientBrush:使用颜色线性渐变填充图形(需要单独引用System.Drawing.Drawing2D)
 6     //设置渐变的起点坐标(0,110),终点坐标(60,110),颜色分别为白色和黑色
 7     LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 110), new Point(60, 110), Color.White, Color.FromArgb(255, 0, 0, 0));
 8     g.FillEllipse(lgb, 0, 80, 60, 30);
 9     lgb.Dispose();
10     g.Dispose();
11 }

绘图结果

4)PathGradientBrush  使用渐变色填充图形,渐变方向是从有路径定义的图形边界指向图形中心

应用实例

 1 private void Form1_Paint(object sender, PaintEventArgs e)
 2 {
 3     Graphics g = e.Graphics;
 4 
 5     //PathGradientBrush:以自定义路径的方式渐变填充图形(需要单独引用System.Drawing.Drawing2D)
 6     //给命名空间改名未避免错误,应该再命名空间上右键->重构->重命名(F2)
 7     GraphicsPath gp = new GraphicsPath();       //创建路径
 8     gp.AddEllipse(0, 80, 60, 30);               //添加路径
 9     PathGradientBrush pgb = new PathGradientBrush(gp);      //根据路径创建笔刷
10     pgb.CenterColor = Color.FromArgb(255, 255, 0, 0);     //设定中间颜色
11     Color[] colors = { Color.FromArgb(255, 0, 255, 0) };
12     pgb.SurroundColors = colors;                //设定环绕颜色
13     e.Graphics.FillEllipse(pgb, 0, 80, 60, 30);     //使用笔刷绘图
14     pgb.Dispose();
15     g.Dispose();
16 }

绘图结果

5)HatchBrush  使用各类图案填充图形

 应用实例

 1  private void Form1_Paint(object sender, PaintEventArgs e)
 2 {
 3     Graphics g = e.Graphics;
 4 
 5     //HatchBrush:使用各类图案填充图形(需要单独引用System.Drawing.Drawing2D)
 6     HatchBrush hb = new HatchBrush(HatchStyle.HorizontalBrick, Color.Red, Color.Yellow);    //第一个参数枚举值可以按需求更改
 7     g.FillEllipse(hb, 0, 80, 60, 30);
 8     hb.Dispose();
 9     g.Dispose();
10 }

绘图结果

4.综合示例:绘制各种形状

 1 private void Form1_Paint(object sender, PaintEventArgs e)
 2 {
 3     Graphics g = e.Graphics;        //获取Graphice画板
 4     Pen p = new Pen(Color.Red, 3);  //建立Pen对象
 5     g.DrawRectangle(p, 10, 10, 40,60);  //绘制左上角坐标(10,10),长宽为(40,60)的矩形
 6 
 7     p.Color = Color.Green;          //更改画笔颜色为绿色
 8     Point[] triangleP = {new Point(50,10),
 9                       new Point(60,70),
10                       new Point(80,40)};
11     g.DrawPolygon(p, triangleP);        //根据给定坐标绘制多边形(三角形)
12 
13     Point[] pentacleP = {new Point(0,76),
14                          new Point(80,76),
15                          new Point(106,0),
16                          new Point(130,76),
17                          new Point(210,76),
18                          new Point(146,124),
19                          new Point(170,200),
20                          new Point(106,152),
21                          new Point(40,200),
22                          new Point(66,124)
23                         };
24     g.DrawPolygon(p, pentacleP);        //绘制正五边形
25 
26     p.Color = Color.Cyan;           //改变画笔颜色
27     g.DrawArc(p, 150, 0, 50, 50, 0, 90);    //绘制圆弧,
28     //(150,0)决定椭圆外切圆左上坐标.(50,50)决定矩形的长宽.(0,90)决定起始角度和扫过的角度
29 
30     g.DrawPie(p, 150, 90, 50, 50, 0, 90);   //绘制扇形,参数列表和绘制圆弧一样
31 
32 
33     p.Dispose();    //释放Pen资源
34     g.Dispose();    //释放Graphics资源
35 }

绘图结果

Ps:学习过程中应用的小技巧。

1.给命名空间改名未避免错误,应该再命名空间上右键->重构->重命名(F2)

2.需要打特殊符号时,如版权符号。可以用win+R调出命令窗口,输入charmap回车,然后再里面选择。

3.使用DrawPolygon方法绘制多边形时,可以先使用其他绘图工具(如CAD)得到多边形的顶点坐标。

  

posted on 2016-10-26 19:45  学习时间轴  阅读(4661)  评论(0编辑  收藏  举报