戏说 .NET GDI+系列学习教程(二、Graphics类的方法)

转载:https://www.cnblogs.com/WarBlog/p/11134146.html

极好的一篇文章!!!

 

一、DrawBezier 画立体的贝尔塞曲线

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    Graphics g = this.CreateGraphics();// e.Graphics;

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

    //从第一个点到第四个点绘制贝塞尔曲线。 第二个和第三个点是确定曲线的形状的控制点。
    Point start = new Point(100, 100);
    Point control1 = new Point(200, 10);
    Point control2 = new Point(350, 50);
    Point end = new Point(500, 100);

    //画立体的贝尔塞曲线.
    //DrawBezier有多种重载这里就不一一说明了
    g.DrawBezier(blackPen, start, control1, control2, end);
}

效果图:

 

 二、DrawArc 画弧

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    Graphics g = this.CreateGraphics();// e.Graphics;

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

    // 定义矩形,用于确定弧线的边界,顾名思义就是在一个定义好的矩形中画画线
    // 现在创建的是一个正方形(正方形画圆,长方形画椭圆)
    Rectangle rect = new Rectangle(10, 10, 100, 100);

    // 从 x 轴到弧线的起始点沿顺时针方向度量的角(以度为单位)
    float startAngle = 45.0F;
    // 从 startAngle 参数到弧线的结束点沿顺时针方向度量的角(以度为单位)
    float sweepAngle = 180.0F;
    // 画矩形
    g.DrawRectangle(p, rect);
    // 画弧.
    g.DrawArc(p, rect, startAngle, sweepAngle);

    p = new Pen(Color.Red, 3);

    // 定义矩形,用于确定弧线的边界,顾名思义就是在一个定义好的矩形中画画线
    // 现在创建的是一个长方形(正方形画圆,长方形画椭圆)
    rect = new Rectangle(200, 200, 200, 100);

    // 从 x 轴到弧线的起始点沿顺时针方向度量的角(以度为单位)
    startAngle = 45.0F;
    // 从 startAngle 参数到弧线的结束点沿顺时针方向度量的角(以度为单位)
    sweepAngle = 180.0F;
    // 画矩形
    g.DrawRectangle(p, rect);
    // 画弧.
    g.DrawArc(p, rect, startAngle, sweepAngle);
}

效果图:

 

 三、DrawClosedCurve 画闭合曲线

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    Graphics g = this.CreateGraphics();// e.Graphics;

    //DrawClosedCurve 画闭合曲线
    Pen redPen = new Pen(Color.Red, 3);
    Pen greenPen = new Pen(Color.Green, 3);

    // 创建七个点来定义一条曲线.
    Point point1 = new Point(50, 50);
    Point point2 = new Point(100, 25);
    Point point3 = new Point(200, 5);
    Point point4 = new Point(250, 50);
    Point point5 = new Point(300, 100);
    Point point6 = new Point(350, 200);
    Point point7 = new Point(250, 250);
    Point[] curvePoints =
              {
          point1,
          point2,
          point3,
          point4,
          point5,
          point6,
          point7
      };

    // 画线.
    e.Graphics.DrawLines(redPen, curvePoints);

    // 画闭合曲线
    e.Graphics.DrawClosedCurve(greenPen, curvePoints);
}

效果图:

 

四、DrawCurve 画曲线

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    Graphics g = this.CreateGraphics();// e.Graphics;

    // Create pens.
    Pen redPen = new Pen(Color.Red, 3);
    Pen greenPen = new Pen(Color.Green, 3);

    // Create points that define curve.
    Point point1 = new Point(50, 50);
    Point point2 = new Point(100, 25);
    Point point3 = new Point(200, 5);
    Point point4 = new Point(250, 50);
    Point point5 = new Point(300, 100);
    Point point6 = new Point(350, 200);
    Point point7 = new Point(250, 250);
    Point[] curvePoints = { point1, point2, point3, point4, point5, point6, point7 };

    // Draw lines between original points to screen.
    e.Graphics.DrawLines(redPen, curvePoints);

    // Draw curve to screen.
    e.Graphics.DrawCurve(greenPen, curvePoints);
}

 

效果图:

 

五、DrawEllipse 画椭圆

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    Graphics g = this.CreateGraphics();// e.Graphics;

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

    // 创建矩形,定义椭圆边界值
    Rectangle rect = new Rectangle(0, 0, 200, 100);

    // 绘制椭圆
    e.Graphics.DrawEllipse(blackPen, rect);
}

效果图:

 

六、DrawImage 画图像 

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    Pen p = new Pen(Color.Red);
    // 创建Image对象
    Image newImage = Image.FromFile(@"E:\Test\Test\GDI_Demo\images\DrawBackgroundImage.png");

    // 创建图像左上角的坐标
    float x = 100.0F;
    float y = 100.0F;

    // 定义矩形的位置(这个和Graphics对象的方法有关)和大小
    // public RectangleF (float x, float y, float width, float height);
    // x,y 矩形左上角坐标
    RectangleF srcRect = new RectangleF(50.0F, 50.0F, 150.0F, 150.0F);
    // 单位:像素
    GraphicsUnit units = GraphicsUnit.Pixel;

    // 矩形左上角坐标,是参照Form窗体的
    e.Graphics.DrawRectangle(p, 50.0F, 50.0F, 150.0F, 150.0F);
    // 将图像绘制到屏幕上
    // 矩形左上角坐标,是参照Image对象的
    e.Graphics.DrawImage(newImage, x, y, srcRect, units);
}

效果图:

说明:

 

 七、DrawLine 画线

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    Pen p = new Pen(Color.Red);
    //起点坐标
    Point point1 = new Point(100, 100);
    //终点坐标
    Point point2 = new Point(500, 100);

    // 绘制直线
    e.Graphics.DrawLine(p, point1, point2);
}

效果图:

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    Pen p = new Pen(Color.Red);
    Point[] points =
     {
         new Point(10,  10),
         new Point(10, 100),
         new Point(200,  50),
         new Point(250, 300)
     };

    //Draw lines to screen.
    e.Graphics.DrawLines(p, points);
}

 

效果图:

 

八、DrawPath 通过路径画线和曲线

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    Pen p = new Pen(Color.Red);
    // 创建图形路径对象,并向它添加一个椭圆
    GraphicsPath graphPath = new GraphicsPath();
    graphPath.AddEllipse(0, 0, 200, 100);
    // 绘制图形路径
    e.Graphics.DrawPath(p, graphPath);
}

 

效果图:

 

九、DrawPie 画饼形

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    Pen p = new Pen(Color.Red);
    // 绘制一个扇形,该形状由一个坐标对、宽度、高度以及两条射线所指定的椭圆定义
    float x = 0.0F;
    float y = 0.0F;
    float width = 200.0F;
    float height = 100.0F;

    // Create start and sweep angles.
    float startAngle = 0.0F;
    float sweepAngle = 90.0F;

    // Draw pie to screen.
    e.Graphics.DrawPie(p, x, y, width, height, startAngle, sweepAngle);
}

 

效果图

说明图

 

 

 

 

 

 

十、DrawPolygon 画多边形 

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    Pen p = new Pen(Color.Red, 3);
    // Create points that define polygon.
    PointF point1 = new PointF(50.0F, 50.0F);
    PointF point2 = new PointF(100.0F, 25.0F);
    PointF point3 = new PointF(200.0F, 5.0F);
    PointF point4 = new PointF(250.0F, 50.0F);
    PointF point5 = new PointF(300.0F, 100.0F);
    PointF point6 = new PointF(350.0F, 200.0F);
    PointF point7 = new PointF(250.0F, 250.0F);
    PointF[] curvePoints =
             {
         point1,
         point2,
         point3,
         point4,
         point5,
         point6,
         point7
     };

    // Draw polygon curve to screen.
    e.Graphics.DrawPolygon(p, curvePoints);
}

 

 效果图:

 

十一、DrawRectangle 画矩形 

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    Pen p = new Pen(Color.Red, 3);
    // Create rectangle.
    Rectangle rect = new Rectangle(0, 0, 200, 200);

    // Draw rectangle to screen.
    e.Graphics.DrawRectangle(p, rect);
}

 效果图:

 

十二、DrawString 绘制文字

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    // 定义绘制文字
    String drawString = "Sample Text";

    // 定义字体
    Font drawFont = new Font("Arial", 16);
    // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画
    // 所以刷子就有以下几种
    // SolidBrush               : 单色画笔
    // TextureBrush             : 纹理画笔(使用图像来填充形状的内部)
    // HatchBrush               : 用阴影样式、前景色和背景色定义矩形画笔
    // LinearGradientBrush      : 线性渐变
    // PathGradientBrush        : 通过渐变填充 GraphicsPath 对象
    SolidBrush drawBrush = new SolidBrush(Color.Black);

    // Create point for upper-left corner of drawing.
    float x = 150.0F;
    float y = 50.0F;

    // 绘制文本的格式化特性(如行距和对齐方式)
    StringFormat drawFormat = new StringFormat();
    drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;

    // Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
}

效果图:

 

十三、FillEllipse 填充椭圆 

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画
    // 所以刷子就有以下几种
    // SolidBrush               : 单色画笔
    // TextureBrush             : 纹理画笔(使用图像来填充形状的内部)
    // HatchBrush               : 用阴影样式、前景色和背景色定义矩形画笔
    // LinearGradientBrush      : 线性渐变
    // PathGradientBrush        : 通过渐变填充 GraphicsPath 对象
    SolidBrush redBrush = new SolidBrush(Color.Red);

    // Create location and size of ellipse.
    int x = 0;
    int y = 0;
    int width = 200;
    int height = 100;

    // 填充边框所定义的椭圆的内部
    e.Graphics.FillEllipse(redBrush, x, y, width, height);
}

效果图:

 

十四、FillPath 填充路径

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画
    // 所以刷子就有以下几种
    // SolidBrush               : 单色画笔
    // TextureBrush             : 纹理画笔(使用图像来填充形状的内部)
    // HatchBrush               : 用阴影样式、前景色和背景色定义矩形画笔
    // LinearGradientBrush      : 线性渐变
    // PathGradientBrush        : 通过渐变填充 GraphicsPath 对象


    //SolidBrush redBrush = new SolidBrush(Color.Red);

    Bitmap image1 = (Bitmap)Image.FromFile(@"E:\Test\Test\GDI_Demo\images\TextureImage.png", true);

    TextureBrush texture = new TextureBrush(image1);
    texture.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;


    // 创建图形路径对象
    GraphicsPath graphPath = new GraphicsPath();
    //向图形路径添加一个椭圆。
    graphPath.AddEllipse(0, 0, 200, 100);

    // 填充 GraphicsPath 
    e.Graphics.FillPath(texture, graphPath);
}

效果图:

 

十五、FillPie 填充饼图 

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画
    // 所以刷子就有以下几种
    // SolidBrush               : 单色画笔
    // TextureBrush             : 纹理画笔(使用图像来填充形状的内部)
    // HatchBrush               : 用阴影样式、前景色和背景色定义矩形画笔
    // LinearGradientBrush      : 线性渐变
    // PathGradientBrush        : 通过渐变填充 GraphicsPath 对象


    //SolidBrush redBrush = new SolidBrush(Color.Red);

    // HatchStyle.Horizontal => 水平线, Color.Red => 红色水平线,背景色:Color.FromArgb(255, 128, 255, 255)
    HatchBrush hBrush = new HatchBrush(HatchStyle.Horizontal, Color.Red, Color.FromArgb(255, 128, 255, 255));

    Rectangle rect = new Rectangle(0, 0, 200, 100);
    // Create start and sweep angles.
    float startAngle = 0.0F;
    float sweepAngle = 45.0F;

    // Fill pie to screen.
    e.Graphics.FillPie(hBrush, rect, startAngle, sweepAngle);
}

效果图:

 

十六、FillPolygon 填充多边形 

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画
    // 所以刷子就有以下几种
    // SolidBrush               : 单色画笔
    // TextureBrush             : 纹理画笔(使用图像来填充形状的内部)
    // HatchBrush               : 用阴影样式、前景色和背景色定义矩形画笔
    // LinearGradientBrush      : 线性渐变
    // PathGradientBrush        : 通过渐变填充 GraphicsPath 对象


    //SolidBrush redBrush = new SolidBrush(Color.Red);

    //坐标相对填充对象
    LinearGradientBrush linGrBrush = new LinearGradientBrush(new Point(0, 10), new Point(200, 10),
                                                               Color.FromArgb(255, 255, 0, 0),   // Opaque red
                                                               Color.FromArgb(255, 0, 0, 255));  // Opaque blue

    Pen pen = new Pen(linGrBrush, 3);

    e.Graphics.DrawLine(pen, 0, 10, 200, 10);

    // Create points that define polygon.
    Point point1 = new Point(50, 50);
    Point point2 = new Point(100, 25);
    Point point3 = new Point(200, 5);
    Point point4 = new Point(250, 50);
    Point point5 = new Point(300, 100);
    Point point6 = new Point(350, 200);
    Point point7 = new Point(250, 250);
    Point[] curvePoints = { point1, point2, point3, point4, point5, point6, point7 };

    // Draw polygon to screen.
    e.Graphics.FillPolygon(linGrBrush, curvePoints);
}

效果图:

 

十七、FillRectangle 填充矩形

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画
    // 所以刷子就有以下几种
    // SolidBrush               : 单色画笔
    // TextureBrush             : 纹理画笔(使用图像来填充形状的内部)
    // HatchBrush               : 用阴影样式、前景色和背景色定义矩形画笔
    // LinearGradientBrush      : 线性渐变
    // PathGradientBrush        : 通过渐变填充 GraphicsPath 对象


    SolidBrush redBrush = new SolidBrush(Color.Red);
    Rectangle rect = new Rectangle(0, 0, 200, 200);
    e.Graphics.FillRectangle(redBrush, rect);
}

效果图:

 

十八、FillRectangles 填充矩形组

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画
    // 所以刷子就有以下几种
    // SolidBrush               : 单色画笔
    // TextureBrush             : 纹理画笔(使用图像来填充形状的内部)
    // HatchBrush               : 用阴影样式、前景色和背景色定义矩形画笔
    // LinearGradientBrush      : 线性渐变
    // PathGradientBrush        : 通过渐变填充 GraphicsPath 对象


    //SolidBrush redBrush = new SolidBrush(Color.Red);
    SolidBrush blueBrush = new SolidBrush(Color.Blue);

    RectangleF[] rects = { new RectangleF(0.0F, 0.0F, 100.0F, 200.0F),
                           new RectangleF(100.0F, 200.0F, 250.0F, 50.0F),
                           new RectangleF(300.0F, 0.0F, 50.0F, 100.0F) };

    e.Graphics.FillRectangles(blueBrush, rects);
}

效果图:

 

十九、FillRegion 填充区域

private void frmGraphics_Paint(object sender, PaintEventArgs e)
{
    // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画
    // 所以刷子就有以下几种
    // SolidBrush               : 单色画笔
    // TextureBrush             : 纹理画笔(使用图像来填充形状的内部)
    // HatchBrush               : 用阴影样式、前景色和背景色定义矩形画笔
    // LinearGradientBrush      : 线性渐变
    // PathGradientBrush        : 通过渐变填充 GraphicsPath 对象

    Graphics g = e.Graphics;
    // 创建矩形,为定义Region对象的填充区域
    Rectangle regionRect = new Rectangle(10, 10, 50, 50);
    Pen pen1 = new Pen(Color.Black);
    //为看出效果
    g.DrawRectangle(pen1, regionRect);

    // 创建第二个矩形,主要用于区域交集填充
    RectangleF unionRect = new RectangleF(25, 25, 50, 50);//第2个矩形
    pen1.Color = Color.Red;
    //为看出效果
    g.DrawEllipse(pen1, unionRect);//画椭圆

    GraphicsPath myPath = new GraphicsPath();
    myPath.AddEllipse(unionRect);

    // 创建Region对象的填充区域
    Region myRegion = new Region(regionRect);

    //两个区域的交集被填充
    myRegion.Intersect(myPath);

    SolidBrush blueBrush = new SolidBrush(Color.Blue);
    // 填充
    e.Graphics.FillRegion(blueBrush, myRegion);

    //=================Complement==================
    regionRect = new Rectangle(100, 10, 50, 50);
    pen1 = new Pen(Color.Black);
    //为看出效果
    g.DrawRectangle(pen1, regionRect);

    // 创建第二个矩形
    unionRect = new RectangleF(115, 25, 50, 50);
    pen1.Color = Color.Red;
    g.DrawEllipse(pen1, unionRect);//画椭圆

    myPath = new GraphicsPath();
    myPath.AddEllipse(unionRect);

    myRegion = new Region(regionRect);
    // myPath 无交集的区域被填充
    myRegion.Complement(myPath);

    // 填充
    e.Graphics.FillRegion(blueBrush, myRegion);

    //=================Exclude==================

    regionRect = new Rectangle(190, 10, 50, 50);
    pen1 = new Pen(Color.Black);
    //为看出效果
    g.DrawRectangle(pen1, regionRect);

    // 创建第二个矩形
    unionRect = new RectangleF(205, 25, 50, 50);
    pen1.Color = Color.Red;
    g.DrawEllipse(pen1, unionRect);//画椭圆

    myPath = new GraphicsPath();
    myPath.AddEllipse(unionRect);

    myRegion = new Region(regionRect);

    // myRegion 无交集的区域被填充
    myRegion.Exclude(myPath);

    // 填充
    e.Graphics.FillRegion(blueBrush, myRegion);

    //=================Union==================

    regionRect = new Rectangle(280, 10, 50, 50);
    pen1 = new Pen(Color.Black);
    //为看出效果
    g.DrawRectangle(pen1, regionRect);

    // 创建第二个矩形
    unionRect = new RectangleF(295, 25, 50, 50);
    pen1.Color = Color.Red;
    g.DrawEllipse(pen1, unionRect);//画椭圆

    myPath = new GraphicsPath();
    myPath.AddEllipse(unionRect);

    myRegion = new Region(regionRect);

    // 两个区域被填充
    myRegion.Union(myPath);

    // 填充
    e.Graphics.FillRegion(blueBrush, myRegion);

    //=================Xor==================

    regionRect = new Rectangle(370, 10, 50, 50);
    pen1 = new Pen(Color.Black);
    //为看出效果
    g.DrawRectangle(pen1, regionRect);

    // 创建第二个矩形
    unionRect = new RectangleF(385, 25, 50, 50);
    pen1.Color = Color.Red;
    g.DrawEllipse(pen1, unionRect);//画椭圆

    myPath = new GraphicsPath();
    myPath.AddEllipse(unionRect);

    myRegion = new Region(regionRect);

    // 交集以外区域被填充
    myRegion.Xor(myPath);

    // 填充
    e.Graphics.FillRegion(blueBrush, myRegion);
}

效果图:

posted @ 2021-05-08 17:03  我得想个好名字  阅读(212)  评论(0)    收藏  举报