C#如何用GDI填充一个圆角矩形
时间: 2023-07-27 15:12:52 浏览: 62
在 C# 中,可以使用 GDI+(Graphics Device Interface)绘制图形。以下是一个示例代码,使用 GDI+ 填充一个圆角矩形:
```C#
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int x = 50;
int y = 50;
int width = 100;
int height = 50;
int radius = 15;
Rectangle rect = new Rectangle(x, y, width, height);
GraphicsPath path = GetRoundedRect(rect, radius);
g.FillPath(Brushes.Blue, path);
}
private GraphicsPath GetRoundedRect(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
int diameter = radius * 2;
Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
path.AddArc(arcRect, 180, 90);
arcRect.X = rect.Right - diameter;
path.AddArc(arcRect, 270, 90);
arcRect.Y = rect.Bottom - diameter;
path.AddArc(arcRect, 0, 90);
arcRect.X = rect.Left;
path.AddArc(arcRect, 90, 90);
path.CloseFigure();
return path;
}
```
在上述代码中,`GetRoundedRect` 方法用于创建一个圆角矩形的路径,而 `FillPath` 方法用于将路径填充为指定的颜色。可以根据需要更改颜色、位置、宽度、高度和半径来适应您的应用程序。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)