三、使用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);
}

绘制效果,左上角为起点。
在这里插入图片描述 在这里插入图片描述

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);
}

在这里插入图片描述

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);
}

在这里插入图片描述

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]);
}

在这里插入图片描述

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);  //调用绘制刻度的函数
}

在这里插入图片描述
还要更多的绘图,读者可以自己试试,利用单纯的基础图形通过一定规则组合成较复杂的图形。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值