GDI+的应用

在VS中创建窗体

(1)CDI+清除绘画面

在窗体中写入代码:

protected override void OnPaint(PaintEventArgs e){

  Graphics g=e.Graphics;

  g.Clear(Color.Pink);

  g.Dispose();

}

(2)CGD+绘制多边形

protected override void OnPaint(PaintEventArgs e){

  Graphics g=e.Graphics;

  Point[] points=new Point[]{

          new Point(200,200),

          new Point(230,230),

          new Point(260,300),

          new Point(300,350)

};

  g.DrawPolygon(new Pen(Color.Red),point);

  g.Dispose();

}

(3)GDI+填充颜色

protected override void OnPaint(PaintEventArgs e){

  //简单填充颜色

  Graphics g=e.Graphics;

  g.FillRectangle(Brushes.Red,new Rectangle(20,20,100,200));

  //渐变颜色填充

  Brush brush=new LinearGradientBrush(new Point(10,10),new Point(10,10),Color.Yellow,Color.White);

   g.FillRectangle(brush,new Rectangle(20,20,100,170));  

  g.Dispose();

}

(4)GDI+绘画路径

protected override void OnPaint(PaintEventArgs e){

   Graphics g = e.Graphics;
            Point[] points = new Point[]{
                 new Point(100,100),
                 new Point(100,150),
               new Point(150,200),
              new Point(50,200),
            };

  GraphicsPath path = new GraphicsPath(

      points,new byte[]{

          (byte)PathPointType.Start,

          (byte)PathPointType.Line,

          (byte)PathPointType.Line,

          (byte)PathPointType.Line 

      );

}

  g.DrawPath(new Pen(Color.Red),path);

  g.Dispose();

}

(4)GDI+绘制字符串

protected override void OnPaint(PaintEventArgs e){

  Graphics g = e.Graphics;

  //普通绘制字符串

  Font font1=new System.Drawing.Font("宋体",30);

  g.DrawingString("ABCD",font1,Brushes.Red,new PointF(30,30));

  //带格式的字符串

         Font font2 = new System.Drawing.Font("宋体", 30);
            RectangleF rect = new RectangleF(100,100,100,200);
            g.DrawRectangle(new Pen(Color.Red),new Rectangle(100,100,100,200));
            //字符串格式对象
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;//在矩形中居中
            sf.LineAlignment = StringAlignment.Center;
            g.DrawString("abcd",font2,Brushes.Red,rect,sf);
            g.Dispose();

}

(5)GDI+纹理绘画图片

 protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Image image = Image.FromFile("图片路径");
            //纹理画笔
            Brush brush = new TextureBrush(image);//刷出来的位置都有image的存在
            g.DrawRectangle(new Pen(Color.Pink),40,40,300,300);
            g.FillRectangle(brush,new Rectangle(40,40,300,300));
            g.Dispose();

        }

转载于:https://www.cnblogs.com/ss977/p/3942428.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值