GDI+——使用Graphics类绘制基本图形

目录

绘制基本图形

绘制直线

绘制矩形

绘制椭圆

绘制圆弧

绘制扇形

绘制多边形

绘制文本

综合案例

绘制波形曲线

使用双缓冲技术绘图


绘制基本图形

绘制直线

绘制直线用到了Graphics类的DrawLine方法。该方法最常用的重载方式有两种,

  1. public void DrawLine(Pen pen,Point pt1,Point pt2)

参数说明:

pen:Pen对象,它确定线条的颜色,宽度和样式。

pt1:Point结构,它表示要连接的第一个点。

pt2:Point结构,表示要连接的第二个点。

代码:

        private void button1_Click(object sender, EventArgs e)

        {

            Pen blackPen = new Pen(Color.Black, 3);//实例化Pen类

            Point point1 = new Point(10, 50);//实例化一个Point类

            Point point2 = new Point(100, 50);//再实例化一个Point类

            Graphics g = this.CreateGraphics();//实例化一个Graphics类

            g.DrawLine(blackPen, point1, point2);//调用DrawLine方法绘制直线

        }

  1. public void DrawLine(Pen pen,int x1,int y1,int x2,int y2)

      pen:System.Drawing.Pen,它确定线条的颜色、宽度和样式。

x1:第一个点的 x 坐标。

y1:第一个点的 y 坐标。

x2:第二个点的 x 坐标。

  y2: 第二个点的 y 坐标。

代码:

            private void button1_Click(object sender, EventArgs e)

        {

            Graphics graphics = this.CreateGraphics();//实例化Graphics类

            Pen myPen = new Pen(Color.Black, 3);//实例化Pen类

            graphics.DrawLine(myPen, 150, 30, 150, 100);//调用DrawLine方法绘制直线

      }

绘制矩形

绘制矩形用到了Graphics类的DrawRectangle方法。常用的重载方式有两种:

  1. public void DrawRectangle (Pen pen, Rectangle rect);

参数

pen:Pen对象,它确定矩形的颜色、宽度和样式。

rect:表示要绘制的矩形的 Rectangle 结构。

代码:

public void DrawRectangleRectangle(PaintEventArgs e)

{

    //创建黑色笔。   

Pen blackPen = new Pen(Color.Black, 3);

    // 创建矩形.

    Rectangle rect = new Rectangle(0, 0, 200, 200);

    // 绘制到屏幕上。

    e.Graphics.DrawRectangle(blackPen, rect);

}

  1. public void DrawRectangle (Pen pen, int x, int y, int width, int height);

参数

pen:Pen,它确定矩形的颜色、宽度和样式。

x:要绘制的矩形的左上角的 x 坐标。

y:要绘制的矩形的左上角的 y 坐标。

width:要绘制的矩形的宽度。

height:要绘制的矩形的高度。

public void DrawRectangleInt(PaintEventArgs e)

{

    //创建黑色笔。

    Pen blackPen = new Pen(Color.Black, 3);

    //创建矩形的位置和大小.

    int x = 0;

    int y = 0;

    int width = 200;

    int height = 200;

    //将矩形绘制到屏幕上.

    e.Graphics.DrawRectangle(blackPen, x, y, width, height);

}

绘制椭圆

绘制椭圆用到了Graphics的DrawEllipse方法。常用的重载如下:

  1. public void DrawEllipse (System.Drawing.Pen pen, System.Drawing.Rectangle rect);

参数

pen:Pen,它确定曲线的颜色、宽度和样式。

rect:Rectangle 结构,它定义椭圆的边界。

代码:

private void DrawEllipseRectangle(PaintEventArgs e)

{

    // Create pen.

    Pen blackPen = new Pen(Color.Black, 3);

    // Create rectangle for ellipse.

    Rectangle rect = new Rectangle(0, 0, 200, 100);

    // Draw ellipse to screen.

    e.Graphics.DrawEllipse(blackPen, rect);

}

  1. public void DrawEllipse (System.Drawing.Pen pen, int x, int y, int width, int height);

参数

pen:Pen,它确定曲线的颜色、宽度和样式。

x:定义椭圆的边框的左上角的 X 坐标。

y:定义椭圆的边框的左上角的 Y 坐标。

width:定义椭圆的边框的宽度。

height:定义椭圆的边框的高度。

代码:

private void DrawEllipseInt(PaintEventArgs e)

{

    // Create pen.

    Pen blackPen = new Pen(Color.Black, 3);

    // Create location and size of ellipse.

    int x = 0;

    int y = 0;

    int width = 200;

    int height = 100;

    // Draw ellipse to screen.

    e.Graphics.DrawEllipse(blackPen, x, y, width, height);

}

绘制圆弧

绘制圆弧用到了Graphics类的DrawArc方法。常用的两种重载方式:

1、public void DrawArc (Pen pen, Rectangle rect, float startAngle, float sweepAngle);

参数

pen:Pen,它确定弧线的颜色、宽度和样式。

rect:RectangleF 结构,它定义椭圆的边界。

startAngle:从 x 轴到弧线的起始点沿顺时针方向度量的角(以度为单位)。

sweepAngle:从 startAngle 参数到弧线的结束点沿顺时针方向度量的角(以度为单位)。

代码:

private void DrawArcRectangle(PaintEventArgs e)

{

   // Create pen.

    Pen blackPen= new Pen(Color.Black, 3);

    // Create rectangle to bound ellipse.

    Rectangle rect = new Rectangle(0, 0, 100, 200);

    // Create start and sweep angles on ellipse.

    float startAngle =  45.0F;

    float sweepAngle = 270.0F;

    // Draw arc to screen.

    e.Graphics.DrawArc(blackPen, rect, startAngle, sweepAngle);

}

  1. public void DrawArc (Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle);

参数

pen:Pen,它确定弧线的颜色、宽度和样式。

x:定义椭圆的矩形的左上角的 x 坐标。

y:定义椭圆的矩形的左上角的 y 坐标。

width:定义椭圆的矩形的宽度。

height:定义椭圆的矩形的高度。

startAngle:从 x 轴到弧线的起始点沿顺时针方向度量的角(以度为单位)。

sweepAngle:从 startAngle 参数到弧线的结束点沿顺时针方向度量的角(以度为单位)。

代码:

private void DrawArcInt(PaintEventArgs e)

{

    // Create pen.

    Pen blackPen= new Pen(Color.Black, 3);

    // Create coordinates of rectangle to bound ellipse.

    int x = 0;

    int y = 0;

    int width = 100;

    int height = 200;

    // Create start and sweep angles on ellipse.

    int startAngle =  45;

    int sweepAngle = 270;

    // Draw arc to screen.

    e.Graphics.DrawArc(blackPen, x, y, width, height, startAngle, sweepAngle);

}

绘制扇形

绘制扇形用到了Graphics的DrawPie方法。常见的重载方式如下:

  1. public void DrawPie (Pen pen, Rectangle rect, float startAngle, float sweepAngle);

参数

pen:Pen,它确定扇形的颜色、宽度和样式。

rect:Rectangle 结构,它表示定义该扇形所属的椭圆的边框。

startAngle:从 x 轴到扇形的第一条边沿顺时针方向度量的角(以度为单位)。

sweepAngle:从 startAngle 参数到扇形的第二条边沿顺时针方向度量的角(以度为单位)

代码:

public void DrawPieRectangle(PaintEventArgs e)

{

    // Create pen.

    Pen blackPen = new Pen(Color.Black, 3);

            

    // Create rectangle for ellipse.

    Rectangle rect = new Rectangle(0, 0, 200, 100);

    // Create start and sweep angles.

    float startAngle =  0.0F;

    float sweepAngle = 45.0F;

    // Draw pie to screen.

    e.Graphics.DrawPie(blackPen, rect, startAngle, sweepAngle);

}

  1. public void DrawPie (Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle);

参数

pen:Pen,它确定扇形的颜色、宽度和样式。

x:边框的左上角的 x 坐标,该边框定义扇形所属的椭圆。

y:边框的左上角的 y 坐标,该边框定义扇形所属的椭圆。

width:边框的宽度,该边框定义扇形所属的椭圆。

height:边框的高度,该边框定义扇形所属的椭圆。

startAngle:从 x 轴到扇形的第一条边沿顺时针方向度量的角(以度为单位)。

sweepAngle:从 startAngle 参数到扇形的第二条边沿顺时针方向度量的角(以度为单位)。

代码:

public void DrawPieInt(PaintEventArgs e)

{

    // Create pen.

    Pen blackPen = new Pen(Color.Black, 3);

    // Create location and size of ellipse.

    int x = 0;

    int y = 0;

    int width = 200;

    int height = 100;

    // Create start and sweep angles.

    int startAngle =  0;

    int sweepAngle = 45;

    // Draw pie to screen.

    e.Graphics.DrawPie(blackPen, x, y, width, height, startAngle, sweepAngle);

}

绘制多边形

绘制多边形用到了Graphics的DrawPolygon方法。常用的重载方法:

  1. public void DrawPolygon (Pen pen, System.Drawing.Point[] points)

参数

pen:Pen,它确定多边形的颜色、宽度和样式。

points:Point 结构数组,这些结构表示多边形的顶点。

代码:

private void button1_Click(object sender, EventArgs e)

        {

            Graphics ghs = this.CreateGraphics();//实例化Graphics类

            Pen myPen = new Pen(Color.Black, 3);//实例化Pen类

            Point point1 = new Point(80, 20);//实例化Point类,表示第1个点

            Point point2 = new Point(40, 50);//实例化Point类,表示第2个点

            Point point3 = new Point(80, 80);//实例化Point类,表示第3个点

            Point point4 = new Point(160, 80);//实例化Point类,表示第4个点

            Point point5 = new Point(200, 50);//实例化Point类,表示第5个点

            Point point6 = new Point(160, 20);//实例化Point类,表示第6个点

            Point[] myPoints ={ point1, point2, point3, point4, point5, point6 };//创建Point结构数组

            ghs.DrawPolygon(myPen, myPoints);//调用Graphics对象的DrawPolygon方法绘制一个多边形

        }

绘制文本

绘制文本用到Graphics的DrawString方法。常用的重载方法:

  1. public void DrawString (string s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.RectangleF layoutRectangle);

参数

s:要绘制的字符串。

font:Font,它定义字符串的文本格式。

brush:Brush,它确定所绘制文本的颜色和纹理。

layoutRectangle:RectangleF 结构,它指定所绘制文本的位置。

代码:

public void DrawStringRectangleF(PaintEventArgs e)

{

    // Create string to draw.

    String drawString = "Sample Text";

    // Create font and brush.

    Font drawFont = new Font("Arial", 16);

    SolidBrush drawBrush = new SolidBrush(Color.Black);

    // Create rectangle for drawing.

    float x = 150.0F;

    float y = 150.0F;

    float width = 200.0F;

    float height = 50.0F;

    RectangleF drawRect = new RectangleF(x, y, width, height);

    // Draw rectangle to screen.

    Pen blackPen = new Pen(Color.Black);

    e.Graphics.DrawRectangle(blackPen, x, y, width, height);

    // Draw string to screen.

    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect);

}

  1. public void DrawString (string s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.PointF point);

参数

s:要绘制的字符串。

font:Font,它定义字符串的文本格式。

brush:Brush,它确定所绘制文本的颜色和纹理。

point:PointF 结构,它指定所绘制文本的左上角。

代码:

public void DrawStringPointF(PaintEventArgs e)

{

    // Create string to draw.

    String drawString = "Sample Text";

    // Create font and brush.

    Font drawFont = new Font("Arial", 16);

    SolidBrush drawBrush = new SolidBrush(Color.Black);

    // Create point for upper-left corner of drawing.

    PointF drawPoint = new PointF(150.0F, 150.0F);

    // Draw string to screen.

    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);

}

  1. public void DrawString(string s, Font font, Brush brush, float x, float y)

参数:

        s:  要绘制的字符串。

        font:System.Drawing.Font,它定义字符串的文本格式。

brush:System.Drawing.Brush,它确定所绘制文本的颜色和纹理。

        x:所绘制文本的左上角的 x 坐标。

        y:所绘制文本的左上角的 y 坐标。

private void button1_Click(object sender, EventArgs e)

        {

            string str = "神马堂;//定义绘制的字符串

            Font myFont = new Font("华文行楷", 20);//实例化Font对象

            SolidBrush myBrush = new SolidBrush(Color.DarkOrange);//实例化画刷对象

            Graphics myGraphics = this.CreateGraphics();//创建Graphics对象

            myGraphics.DrawString(str, myFont, myBrush,10,20);//绘制文本

        }

综合案例

绘制波形曲线

 

   GDI+——使用Graphics类绘制基本图形

private void button1_Click(object sender, EventArgs e)

   {

            Graphics graphics = this.CreateGraphics();                  //实例化窗体的Graphics类

            Pen myPen = new Pen(Color.Black, 1);                        //设置画笔

            int beginX = 50;                                        //定义变量

            int beginY = 65;

            int height = 35;

            int width = 50;

            Point pointX1 = new Point(beginX, beginY);

            Point pointY1 = new Point(beginX + 210, beginY);

            Point pointX2 = new Point(beginX, beginY - 45);

            Point pointY2 = new Point(beginX, beginY + 45);

            //调用DrawLine方法绘制两条垂直相交的直线,用来作为波形图的横纵坐标

            graphics.DrawLine(myPen, pointX1, pointY1);

            graphics.DrawLine(myPen, pointX2, pointY2);

//绘制上半区域交错连接的贝塞尔曲线

            graphics.DrawBezier(myPen, beginX, beginY, beginX + 15, beginY - height, beginX + 40, beginY - height, beginX + width,beginY);   

//绘制下半区域交错连接的贝塞尔曲线                                 graphics.DrawBezier(myPen, beginX + width, beginY, beginX + width + 15, beginY + height, beginX + width + 40,beginY + height, beginX + width * 2, beginY);  

//绘制上半区域交错连接的贝塞尔曲线                 

graphics.DrawBezier(myPen, beginX + width * 2, beginY, beginX + width * 2 + 15, beginY - height, beginX + width * 2+ 40, beginY - height, beginX + width * 3, beginY);

/绘制下半区域交错连接的贝塞尔曲线                               graphics.DrawBezier(myPen, beginX + width * 3, beginY, beginX + width * 3 + 15, beginY + height, beginX + width * 3+ 40, beginY + height, beginX + width * 4, beginY);              /

  }

使用双缓冲技术绘图

双缓冲即在内存中创建一个与屏幕绘图区域一致的对象,先将图形绘制到内存中的这个对象上,再一次性将这个对象上的图形拷贝到屏幕上,这样能大大加快绘图的速度。主要用来解决窗体大小改变时,出现窗体闪烁的问题。

双缓冲实现过程如下:

1、在内存中创建与画布一致的缓冲区

2、在缓冲区画图

3、将缓冲区位图拷贝到当前画布上

4、释放内存缓冲区

代码:

    public partial class Frm_Main : Form

    {

        public Frm_Main()

        {

            InitializeComponent();

        }

        private void PaintImage(Graphics g)

        {

            //绘图

            GraphicsPath path = new GraphicsPath(new Point[]{ new Point(100,60),new Point(350,200),new Point(105,225),new Point(190,ClientRectangle.Bottom),

                new Point(50,ClientRectangle.Bottom),new Point(50,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.LightBlue };

            g.FillPath(pgb, path);

            g.DrawString("神马堂欢迎你!!", new Font("宋体", 18, FontStyle.Bold), new SolidBrush(Color.Red), new PointF(110, 20));

            g.DrawBeziers(new Pen(new SolidBrush(Color.Green),2),new Point[] {new Point(220,100),new Point(250,180),new Point(300,70),new Point(350,150)});

            g.DrawArc(new Pen(new SolidBrush(Color.Blue), 5), new Rectangle(new Point(250, 170), new Size(60, 60)), 0, 360);

            g.DrawRectangle(new Pen(new SolidBrush(Color.Orange), 3), new Rectangle(new Point(240, 260), new Size(90, 50)));

        }

 

        private void Form1_Paint(object sender, PaintEventArgs e)

        {

            Bitmap localBitmap = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);

            //创建位图实例

            Graphics bitmapGraphics = Graphics.FromImage(localBitmap);

            bitmapGraphics.Clear(BackColor);

            bitmapGraphics.SmoothingMode = SmoothingMode.AntiAlias;

            PaintImage(bitmapGraphics);

            Graphics g = e.Graphics;//获取窗体画布

            g.DrawImage(localBitmap, 0, 0); //在窗体的画布中绘画出内存中的图像

            bitmapGraphics.Dispose();

            localBitmap.Dispose();

            g.Dispose();

        }

  }

效果:

 

GDI+——使用Graphics类绘制基本图形