1.概要
使用System.Drawing进行几何图形绘制
System.Drawing
是.NET框架中的一个命名空间,提供了基本的绘图功能,包括绘制几何图形(如矩形、椭圆、线条等)。- 它通常用于Windows Forms应用程序中的绘图。
- 你可以使用
Graphics
类来绘制几何图形。
2.代码
using System;
using System.Drawing;
using System.Windows.Forms;
namespace GeometricShapesExample
{
public class GeometricShapesForm : Form
{
public GeometricShapesForm()
{
this.Width = 800;
this.Height = 600;
this.Paint += new PaintEventHandler(Form_Paint);
}
private void Form_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// 创建一个矩形
Rectangle rect = new Rectangle(50, 50, 200, 100);
// 填充矩形
g.FillRectangle(Brushes.Blue, rect);
// 绘制矩形边界
g.DrawRectangle(Pens.Black, rect);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new GeometricShapesForm());
}
}
}
3.运行结果