c# gdi怎么使用

C# GDI(Graphics Device Interface)是一种用于绘制图形和图像的API。下面是使用C# GDI绘制图形的一些基本步骤:

引入命名空间:

using System.Drawing;
using System.Drawing.Drawing2D;

创建一个Graphics对象:

Graphics graphics = this.CreateGraphics();

创建一个画笔(Pen)或刷子(Brush)对象,用于指定绘制的颜色和样式:

Pen pen = new Pen(Color.Red, 2); // 创建红色画笔,线宽为2个像素
Brush brush = new SolidBrush(Color.Blue); // 创建蓝色刷子

绘制图形:

graphics.DrawLine(pen, startPoint, endPoint); // 绘制直线
graphics.DrawRectangle(pen, x, y, width, height); // 绘制矩形
graphics.DrawEllipse(pen, x, y, width, height); // 绘制椭圆
graphics.FillRectangle(brush, x, y, width, height); // 填充矩形
graphics.FillEllipse(brush, x, y, width, height); // 填充椭圆

释放资源:

pen.Dispose();
brush.Dispose();
graphics.Dispose();

注意:以上代码片段仅为示例,实际使用时需要根据具体需求进行调整。

阅读剩余
THE END