winform GDI基础(一)

1获取画布

(1)从PaintEventArgs类中获取画布

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
        }

(2)从Image中获取画布

            Bitmap bm = new Bitmap(this.Width, this.Height);
            Graphics g = Graphics.FromImage(bm);

(3)使用CreateGraphics创建画布

 Graphics g = this.CreateGraphics();

(4)其他方式

从设备上下文的指定句柄创建新的 System.Drawing.Graphics

public static Graphics FromHdc(IntPtr hdc);

从设备上下文的指定句柄和设备的句柄创建新的 System.Drawing.Graphics

public static Graphics FromHdc(IntPtr hdc, IntPtr hdevice);

返回指定设备上下文的 System.Drawing.Graphics

public static Graphics FromHdcInternal(IntPtr hdc);

从窗口的指定句柄创建新的 System.Drawing.Graphics

public static Graphics FromHwnd(IntPtr hwnd);

创建指定 Windows 句柄的新 System.Drawing.Graphics

 public static Graphics FromHwndInternal(IntPtr hwnd);

2绘制图像

            Graphics g = this.CreateGraphics();
            System.Drawing.Pen pen = new System.Drawing.Pen(Color.Red, 2);
            g.DrawLine();//画线
            g.DrawRectangle();//画矩形
            g.DrawEllipse();//画椭圆
            g.DrawArc();//画扇形

3抗锯齿处理

g.SmoothingMode = SmoothingMode.HighQuality;//设置抗锯齿
g.CompositingQuality = CompositingQuality.HighQuality;//提升合成图片质量
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;//去掉文字的锯齿

4处理图像

            Bitmap bm = new Bitmap(200, 200);
            Graphics g = Graphics.FromImage(bm);
            System.Drawing.Pen pen = new System.Drawing.Pen(Color.Red, 4);
            g.DrawLine(pen,new Point(10,10),new Point(100,100));
            bm.Save(Application.StartupPath + @"\1.png");
            g.Dispose();
            bm.Dispose();

  

 

转载于:https://www.cnblogs.com/yaosj/p/10489979.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值