C# GDI+ Graphics 教程

一 GDI+概述

GDI+是微软在Windows 2000以后操作系统中提供的新的图形设备接口,其通过一套部署为托管代码的类来展现,这套类被称为GDI+的“托管类接口”,GDI+主要提供了以下三类服务:

(1) 二维矢量图形:GDI+提供了存储图形基元自身信息的类(或结构体)、存储图形基元绘制方式信息的类以及实际进行绘制的类。

(2) 图像处理:大多数图片都难以划定为直线和曲线的集合,无法使用二维矢量图形方式进行处理。因此,GDI+为我们提供了Bitmap、Image等类,它们可用于显示、操作和保存BMP、JPG、GIF等图像格式。

(3) 文字显示:GDI+支持使用各种字体、字号和样式来显示文本。

我们要进行图形编程,就必须先讲解Graphics类,同时我们还必须掌握Pen(钢笔)、Brush(刷子)和Rectangle(矩形)这几种类。

GDI+比GDI优越主要表现在两个方面:

       1.GDI+通过提供新功能(例如:渐变画笔和alpha混合)扩展了GDI的功能;

       2.修订了编程模型,使图形编程更加简易灵活。

二 Graphics类

画图方法都被包括在Graphics类中,在画任何对象(例如:Circle,Rectangle)时,我们首先要创建一个Graphics类实例,这个实例相当于建立了一块画布,有了画布才可以用各种画图方法进行绘图。

绘图程序的设计过程一般分为两个步骤:(一)创建Graphics对象;(二)使用Graphics对象的方法绘图、显示文本或处理图像。

2.1 获取Graphics对象的三种方法

    2.1.1 利用控件或窗体的Paint事件中的PainEventArgs

           a.用控件或窗体的Paint事件

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
        }

        private void GDI01Form_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
        }

         b.重写窗体的Paint事件

protected override void OnPaint(PaintEventArgs e)
{
      Graphics g = e.Graphics;
}

     2.1.2 调用某控件或窗体的CreateGraphics方法

 Graphics g = this.CreateGraphics();
 Graphics pg = this.panel1.CreateGraphics();

       2.1.3 调用Graphics类的FromImage静态方法

Image img = Image.FromFile("g1.bmp");
Graphics g = Graphics.FromImage(img);// = Graphics.FromImage(this.pictureBox1.Image)

 2.2  Graphics类的成员方法

方法名称参数说明
DrawArc绘制一段弧线,它表示 System.Drawing.Rectangle 结构指定的椭圆的一部分。参数:
        //   pen:
        //     System.Drawing.Pen,它确定弧线的颜色、宽度和样式。
        //
        //   rect:
        //     System.Drawing.RectangleF 结构,它定义椭圆的边界。
        //
        //   startAngle:
        //     从 x 轴到弧线的起始点沿顺时针方向度量的角(以度为单位)。
        //
        //   sweepAngle:
        //     从 startAngle 参数到弧线的结束点沿顺时针方向度量的角(以度为单位)。
DrawBezier(Pen pen, PointF pt1, PointF pt2, PointF pt3, PointF pt4);绘制由四个表示点的有序坐标对定义的贝塞尔样条。// 参数:
        //   pen:
        //     System.Drawing.Pen,它确定曲线的颜色、宽度和样式。
        //
        //   pt1:
        //     System.Drawing.PointF 结构,它表示曲线的起始点。
        //
        //   pt2:
        //     System.Drawing.PointF 结构,它表示曲线的第一个控制点。
        //
        //   pt3:
        //     System.Drawing.PointF 结构,它表示曲线的第二个控制点。
        //
        //   pt4:
        //     System.Drawing.PointF 结构,它表示曲线的结束点。
DrawCurve(Pen pen, Point[] points);绘制经过一组指定的 System.Drawing.Point 结构的基数样条。//   pen:
        //     System.Drawing.Pen,它确定曲线的颜色、宽度和高度。
        //
        //   points:
        //     System.Drawing.Point 结构数组,这些结构定义样条。
 DrawEllipse(Pen pen, Rectangle rect);绘制边界 System.Drawing.Rectangle 结构指定的椭圆。 pen:
        //     System.Drawing.Pen,它确定曲线的颜色、宽度和样式。
        //
        //   rect:
        //     System.Drawing.Rectangle 结构,它定义椭圆的边界。
DrawImage(Image image, int x, int y)在由坐标对指定的位置,使用图像的原始物理大小绘制指定的图像。 //   image:
        //     要绘制的 System.Drawing.Image。
        //
        //   x:
        //     所绘制图像的左上角的 x 坐标。
        //
        //   y:
        //     所绘制图像的左上角的 y 坐标。
void DrawPie(Pen pen, Rectangle rect, float startAngle, float sweepAngle); 绘制由一个 System.Drawing.Rectangle 结构和两条射线所指定的椭圆定义的扇形。 // 参数:
        //   pen:
        //     System.Drawing.Pen,它确定扇形的颜色、宽度和样式。
        //
        //   rect:
        //     System.Drawing.Rectangle 结构,它表示定义该扇形所属的椭圆的边框。
        //
        //   startAngle:
        //     从 x 轴到扇形的第一条边沿顺时针方向度量的角(以度为单位)。
        //
        //   sweepAngle:
        //     从 startAngle 参数到扇形的第二条边沿顺时针方向度量的角(以度为单位)。
DrawPolygon(Pen pen, Point[] points); 绘制由一组 System.Drawing.Point 结构定义的多边形。 //   pen:
        //     System.Drawing.Pen,它确定多边形的颜色、宽度和样式。
        //
        //   points:
        //     System.Drawing.Point 结构数组,这些结构表示多边形的顶点。
void DrawRectangle(Pen pen, Rectangle rect);绘制由 System.Drawing.Rectangle 结构指定的矩形。//   pen:
        //     System.Drawing.Pen,它确定矩形的颜色、宽度和样式。
        //
        //   rect:
        //     表示要绘制的矩形的 System.Drawing.Rectangle 结构。
 DrawString(string s, Font font, Brush brush, float x, float y); 在指定位置并且用指定的 System.Drawing.Brush 和 System.Drawing.Font 对象绘制指定的文本字符串。 // 参数:
        //   s:
        //     要绘制的字符串。
        //
        //   font:
        //     System.Drawing.Font,它定义字符串的文本格式。
        //
        //   brush:
        //     System.Drawing.Brush,它确定所绘制文本的颜色和纹理。
        //
        //   x:
        //     所绘制文本的左上角的 x 坐标。
        //
        //   y:
        //     所绘制文本的左上角的 y 坐标。
FillEllipse(Brush brush, Rectangle rect); 填充 System.Drawing.Rectangle 结构指定的边框所定义的椭圆的内部。//   brush:
        //     确定填充特性的 System.Drawing.Brush。
        //
        //   rect:
        //     System.Drawing.Rectangle 结构,它表示定义椭圆的边框。
void FillPie(Brush brush, Rectangle rect, float startAngle, float sweepAngle);填充椭圆所定义的扇形区的内部,该椭圆由 System.Drawing.RectangleF 结构和两条射线指定。//   brush:
        //     确定填充特性的 System.Drawing.Brush。
        //
        //   rect:
        //     System.Drawing.Rectangle 结构,它表示定义该扇形区所属的椭圆的边框。
        //
        //   startAngle:
        //     从 x 轴沿顺时针方向旋转到扇形区第一个边所测得的角度(以度为单位)。
        //
        //   sweepAngle:
        //     从 startAngle 参数沿顺时针方向旋转到扇形区第二个边所测得的角度(以度为单位)。
void FillPolygon(Brush brush, Point[] points);填充 System.Drawing.Point 结构指定的点数组所定义的多边形的内部。//   brush:
        //     确定填充特性的 System.Drawing.Brush。
        //
        //   points:
        //     System.Drawing.Point 结构数组,这些结构表示要填充的多边形的顶点。

   2.3 Graphics常用的画图对象

      Pen:用来用patterns、colors或者bitmaps进行填充。

      Color:用来画线和多边形,包括矩形、圆和饼形。

      Font:用来给文字设置字体格式。

      Brush:用来描述颜色。

      Rectangle:矩形结构通常用来在窗体上画矩形。

     Point:描述一对有序的x,y两个坐标值。

   2.4   基本图形绘制举例

      2.4.1 画一个矩形

         

protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle rect = new Rectangle(20, 30, 100, 100);
            // LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Red, Color.Yellow);
            SolidBrush sdBrush1 = new SolidBrush(Color.Red);
            g.FillRectangle(sdBrush1, rect);
        }

    2.4.2 画一个弧

   

 protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pn = new Pen(Color.Blue);
            Rectangle rect = new Rectangle(50, 50, 200, 100);
            g.DrawArc(pn, rect, 12, 84);
        }

  2.4.3 画一个椭圆

 protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pn = new Pen(Color.Blue, 10);
            Rectangle rect = new Rectangle(50, 50, 200, 200);
            g.DrawEllipse(pn, rect);
        }

  2.4.4 填充路径

  

 

 protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            // 填充工作区域白色
            g.FillRectangle(new SolidBrush(Color.White),this.ClientRectangle);
            GraphicsPath path = new GraphicsPath(new Point[] {
              new Point(40, 140), new Point(275, 200),
              new Point(105, 225), new Point(190, 300),
              new Point(50, 350), new Point(20, 180), },
            new byte[] {
              (byte)PathPointType.Start,
              (byte)PathPointType.Bezier,
              (byte)PathPointType.Bezier,
              (byte)PathPointType.Bezier,
              (byte)PathPointType.Line,
              (byte)PathPointType.Line,
            });

            PathGradientBrush pgb = new PathGradientBrush(path);
            pgb.SurroundColors = new Color[]
            {
                Color.Green,Color.Yellow,Color.Red, Color.Blue,
                Color.Orange, Color.White,
            };
            g.FillPath(pgb, path);
        }

 

 

 

 

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值