c# graphics画圆
时间: 2023-09-04 12:15:17 浏览: 56
在 C# 中使用 Graphics 类可以很容易地绘制圆形。以下是一个简单的示例代码:
```
private void DrawCircle(PaintEventArgs e)
{
Pen pen = new Pen(Color.Black, 2);
int x = 50;
int y = 50;
int diameter = 100;
e.Graphics.DrawEllipse(pen, x, y, diameter, diameter);
}
```
这个示例代码会在画布上绘制一个直径为 100 像素的黑色圆形,圆心位于 (50,50) 坐标位置。你可以在你的程序中调用这个方法,把它传递给 PaintEventArgs 参数。例如,在你的 Windows 窗体上创建一个 Paint 事件处理程序:
```
private void Form1_Paint(object sender, PaintEventArgs e)
{
DrawCircle(e);
}
```
这将在窗体上绘制一个圆形。你可以根据需要自定义圆形的位置、大小和颜色。
相关问题
C# region画圆
在C#中,可以使用System.Drawing命名空间下的Graphics类和Pen类来画圆。以下是一个简单的示例代码:
```csharp
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Black, 2); // 定义黑色粗线笔
int x = 50; // 圆心的横坐标
int y = 50; // 圆心的纵坐标
int r = 30; // 圆的半径
g.DrawEllipse(pen, x - r, y - r, 2 * r, 2 * r); // 以(x,y)为圆心,r为半径画圆
}
```
在这个示例中,我们在PictureBox的Paint事件中使用Graphics类和Pen类来绘制圆形。其中,Pen类用于定义画笔的样式,DrawEllipse方法用于绘制圆形。你可以根据自己的需求修改圆心坐标和半径大小来画出不同的圆形。
c#怎么根据轴径画圆
在 C# 中,你可以使用 `Graphics` 类的 `DrawEllipse` 方法来根据轴径绘制圆。下面是一个示例代码:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
public class CircleForm : Form
{
public CircleForm()
{
this.Paint += new PaintEventHandler(DrawCircle);
}
private void DrawCircle(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Black, 2);
int centerX = this.Width / 2;
int centerY = this.Height / 2;
int radiusX = 100; // 横向轴径
int radiusY = 100; // 纵向轴径
int x = centerX - radiusX;
int y = centerY - radiusY;
int width = radiusX * 2;
int height = radiusY * 2;
g.DrawEllipse(pen, x, y, width, height);
}
public static void Main()
{
Application.Run(new CircleForm());
}
}
```
这段代码创建了一个继承自 `Form` 的自定义窗体 `CircleForm`,并在窗体上绘制了一个椭圆(即轴径不相等的圆)。你可以根据需要修改 `radiusX` 和 `radiusY` 变量的值来调整椭圆的轴径。运行程序后,将显示一个带有绘制的圆的窗体。