戏说 .NET GDI+系列学习教程(一、Graphics类--纸)

Graphics类(纸)

Graphics类封装一个GDI+绘图图面,提供将对象绘制到显示设备的方法,Graphics与特定的设备上下文关联。
画图方法都被包括在Graphics类中,在画任何对象时,我们首先要创建一个Graphics类实例,这个实例相当于建立了一块画布,有了画布才可以用各种画图方法进行绘图。
 
现实中作画或写字,都离不开笔(Pen)、墨(Brush)、纸(Graphics)、砚,那么今天说的Graphics类就相当于纸。

绘图程序的设计过程一般分为两个步骤:

  1. 创建Graphics对象;
  2. 使用Graphics对象的方法绘图、显示文本或处理图像。

创建Graphics对象

方法一、利用控件或窗体的Paint事件中的PainEventArgs 

 1         /// <summary>
 2         /// 重绘Form窗体时,触发该事件
 3         /// 控件也有Paint事件(如:private void button1_Paint(object sender, PaintEventArgs e))
 4         /// </summary>
 5         /// <param name="sender"></param>
 6         /// <param name="e"></param>
 7         private void frmGraphics_Paint(object sender, PaintEventArgs e)
 8         {
 9             Graphics g = e.Graphics;
10         }
        /// <summary>
        /// 也可以直接重载控件或Form窗体的OnPaint方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
        }

自定义控件重载OnPaint方法

    /// <summary>
    /// 自定义控件
    /// </summary>
    class CustomizeControl: System.Windows.Forms.Control
    {
        /// <summary>
        /// 也可以直接重载控件或Form窗体的OnPaint方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
        }
    }

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

        /// <summary>
        /// 如果想在已存在的窗体或控件上绘图,通常会使用此方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmGraphics_Paint(object sender, PaintEventArgs e)
        {
            //this代表Form窗体
            Graphics g = this.CreateGraphics();

            //button1控件对象
            g = button1.CreateGraphics();
        }

 方法三、调用Graphics类的FromImage静态方法 

            //1.建立Image对象
            //名为“graphics.png”的图片位于当前路径下
            Image img = Image.FromFile(@"E:TestTestDrawDemoImagesgraphics.png", false); //建立Image对象
            img = new Bitmap(@"E:TestTestDrawDemoImagesgraphics.png");

            //实例化一个内存流--->把从文件流中读取的内容[字节数组]放到内存流中去
            System.IO.FileStream fs = new System.IO.FileStream("graphics.png", System.IO.FileMode.Open);

            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);
            fs.Close();
            //实例化一个内存流--->把从文件流中读取的内容[字节数组]放到内存流中去
            System.IO.MemoryStream ms = new System.IO.MemoryStream(data);
            img = Image.FromStream(ms);

            //2.创建Graphics对象
            Graphics g = Graphics.FromImage(img);

Graphics类常用方法 

名称

说明

DrawBezier

画立体的贝尔塞曲线

DrawArc

画弧

DrawBeziers

画连续立体的贝尔塞曲线

DrawClosedCurve

画闭合曲线

DrawCurve

画曲线

DrawEllipse

画椭圆

DrawImage

画图像

DrawLine

画线

DrawPath

通过路径画线和曲线

DrawPie

画饼形

DrawPolygon

画多边形

DrawRectangle

画矩形

DrawString

绘制文字

FillEllipse

填充椭圆

FillPath

填充路径

FillPie

填充饼图

FillPolygon

填充多边形

FillRectangle

填充矩形

FillRectangles

填充矩形组

FillRegion

填充区域

原文地址:https://www.cnblogs.com/WarBlog/p/11127942.html