三、使用GDI+进行绘图

基础图元的绘图过程

1. 画直线

private void Form_Paint(object sender, PaintEventArgs e)
{
    Graphics g = this.CreateGraphics();
    PointF p1 = new PointF(100, 100);
    PointF p2 = new PointF(300, 300);
    Pen pen =new Pen(Brushes.Red);   //选择红色笔
    g.DrawLine(pen, p1, p2);
}

绘制效果,左上角为起点。
三、使用GDI+进行绘图_第1张图片 三、使用GDI+进行绘图_第2张图片

2. 画三角形

private void Form_Paint(object sender, PaintEventArgs e)
{
    Graphics g = this.CreateGraphics();
    Pen pen =new Pen(Brushes.Black);
    PointF[] points = { new PointF(100, 100), new PointF(200, 100), new PointF(200, 300) 
    g.DrawPolygon(pen, points);
}

三、使用GDI+进行绘图_第3张图片

3. 画弧线

private void Form_Paint(object sender, PaintEventArgs e)
{
   Graphics g = this.CreateGraphics();
    Pen pen =new Pen(Brushes.Blue);
    SizeF size = new SizeF(300, 300);
    PointF p = new PointF(0, 0);
    RectangleF rec = new RectangleF(p, size);
    g.DrawArc(pen, rec, 0, 120);
}

三、使用GDI+进行绘图_第4张图片

4. 画棱柱

private void Form_Paint(object sender, PaintEventArgs e)
{
   Graphics g = this.CreateGraphics();
   float width =150;
   float height =150;
   float x1 = 150;
   float y1 = 150;
   Pen pen =new Pen(Brushes.DarkGreen);
   PointF[] points = { new PointF(x1, y1), //起点为面前四边形中左上角的顶点
                       new PointF(x1 , y1 + height), 
                       new PointF(x1 +  0.6f*width, y1+height),
                       new PointF(x1 +  0.6f*width, y1),
                       new PointF(x1 , y1),
                       new PointF(x1 - 0.2f * width, y1 - width*0.4f),
                       new PointF(x1 + 0.3f * width, y1 - width*0.8f),
                       new PointF(x1 + 0.8f * width, y1-width*0.4f),
                       new PointF(x1 +  0.6f*width, y1),
                       new PointF(x1 +  0.6f*width, y1+height),
                       new PointF(x1 +  0.8f*width, y1+height-width*0.4f),
                       new PointF(x1 + 0.8f * width, y1-width*0.4f),
                       new PointF(x1 +  0.6f*width, y1),
                       new PointF(x1 , y1),
                       new PointF(x1 , y1 + height), 
                       new PointF(x1-0.2f*width , y1 + height-width*0.4f), 
                       new PointF(x1 - 0.2f * width, y1 - width*0.4f)
                    };
            g.DrawPolygon(pen, points);
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            PointF c = new PointF(x1 + 0.3f * width, y1 - width * 0.8f + height);
            g.DrawLine(pen, c, points[6]);
            g.DrawLine(pen, c, points[10]);
            g.DrawLine(pen, c, points[15]);
}

三、使用GDI+进行绘图_第5张图片

5. 画直尺

//画刻度的函数
 protected void ScaleMark(Graphics gra, float x1, float y1, float width, float height, int scale, bool isDown = false, float size = 9)
        {
            float xOffset = x1;
            float yOffset = y1;
            float offset = 0;
            Pen pen = new Pen(Brushes.DarkGreen);
            StringFormat strfmt = new StringFormat();
            strfmt.Alignment = StringAlignment.Center;
            for (int i = 0; i <= scale; i++)
            {
                offset += width / scale;
                if (i % 10 == 0)
                {
                    gra.DrawLine(pen,
                       new PointF(xOffset + offset, yOffset),
                       new PointF(xOffset + offset, yOffset + (isDown == false ? (10 + height / 5) : (-height / 5 - 10))));

                    gra.DrawString((i / 10).ToString(), new Font("宋体", size), Brushes.Black,
                        new PointF(xOffset + offset, yOffset + (isDown == false ? (+10 + height / 5) : (-20 - height / 5))),
                         strfmt);
                }
                else if (i % 5 == 0)
                {
                    gra.DrawLine(pen,
                        new PointF(xOffset + offset, yOffset),
                        new PointF(xOffset + offset, yOffset + (isDown == false ? (7 + height / 8) : (-7 - height / 8))));
                }
                else
                {
                    gra.DrawLine(pen,
                     new PointF(xOffset + offset, yOffset),
                      new PointF(xOffset + offset, yOffset + (isDown == false ? (+5 + height / 10) : (-5 - height / 10))));
                }
            }
        }
private void Form_Paint(object sender, PaintEventArgs e)
{
   Graphics g = this.CreateGraphics();
           float x1 = 150;
           float y1 = 150;
           float size = 50;
            Pen pen =new Pen(Brushes.DarkGreen);
           float width = size * 10;
           float height = size;
            g.DrawPolygon(pen, new PointF[] 
            {
              new PointF(x1,y1),
              new PointF(x1+ width, y1),
              new PointF(x1 + width, y1 + height),
              new PointF(x1,y1 + height)
            });
            ScaleMark(g, x1 + 1, y1 , width - size / 2, height, 100, false);  //调用绘制刻度的函数
}

三、使用GDI+进行绘图_第6张图片
还要更多的绘图,读者可以自己试试,利用单纯的基础图形通过一定规则组合成较复杂的图形。

你可能感兴趣的:(GDI/GDI+,gdi/gdi+,c#,图形学,经验分享,其他)