C#以坐标填充颜色
时间: 2023-07-12 16:06:03 浏览: 74
在 C# 中,可以使用 System.Drawing 命名空间中的 Graphics 类来绘制图形并填充颜色。下面是一个简单的示例代码,演示如何使用 Graphics 类在 Windows 窗体应用程序中以坐标填充颜色。
```csharp
using System.Drawing;
using System.Windows.Forms;
namespace FillColorExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
// 创建 Graphics 对象
Graphics g = e.Graphics;
// 定义颜色和矩形区域
Color color = Color.Red;
Rectangle rect = new Rectangle(50, 50, 100, 100);
// 填充矩形区域
SolidBrush brush = new SolidBrush(color);
g.FillRectangle(brush, rect);
}
}
}
```
在上面的代码中,我们在窗体的 Form1_Paint 事件中创建了一个 Graphics 对象,并使用 FillRectangle 方法填充了矩形区域。可以根据需要更改颜色、位置和大小来绘制任何形状。