c# picturebox 在制定坐标画点
时间: 2023-08-02 18:28:47 浏览: 67
在 C# 中使用 PictureBox 绘制点,可以使用 Graphics 类的 DrawEllipse 或 DrawRectangle 方法。
以下是使用 DrawEllipse 方法在指定坐标画点的示例代码:
```csharp
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
int x = 50; // 点的 x 坐标
int y = 50; // 点的 y 坐标
int size = 5; // 点的大小
// 创建画笔
Pen pen = new Pen(Color.Black, size);
// 在指定坐标画点
e.Graphics.DrawEllipse(pen, x - size / 2, y - size / 2, size, size);
}
```
在上述代码中,需要在 PictureBox 的 Paint 事件中绘制点。在绘制点时,需要指定点的坐标和大小,然后使用 Graphics 类的 DrawEllipse 方法绘制点。注意,点的大小应该是奇数,因为 DrawEllipse 方法绘制的是椭圆,而不是圆形。
如果要绘制多个点,可以在 Paint 事件中使用一个循环来绘制。例如,以下代码绘制了 10 个随机位置的点:
```csharp
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
int size = 5; // 点的大小
Pen pen = new Pen(Color.Black, size);
Random random = new Random();
for (int i = 0; i < 10; i++)
{
int x = random.Next(pictureBox1.Width);
int y = random.Next(pictureBox1.Height);
e.Graphics.DrawEllipse(pen, x - size / 2, y - size / 2, size, size);
}
}
```