winform GDI基础(一)
1获取画布
(1)从PaintEventArgs类中获取画布
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; }
(2)从Image中获取画布
1 2 | Bitmap bm = new Bitmap( this .Width, this .Height); Graphics g = Graphics.FromImage(bm); |
(3)使用CreateGraphics创建画布
1 | 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绘制图像
1 2 3 4 5 6 | Graphics g = this .CreateGraphics(); System.Drawing.Pen pen = new System.Drawing.Pen(Color.Red, 2); g.DrawLine(); //画线 g.DrawRectangle(); //画矩形 g.DrawEllipse(); //画椭圆 g.DrawArc(); //画扇形 |
3抗锯齿处理
1 2 3 | g.SmoothingMode = SmoothingMode.HighQuality; //设置抗锯齿 g.CompositingQuality = CompositingQuality.HighQuality; //提升合成图片质量 g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; //去掉文字的锯齿 |
4处理图像
1 2 3 4 5 6 7 | 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(); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本