c#imge设置圆角
时间: 2023-12-07 16:39:26 浏览: 77
以下是在C#中设置图片圆角的示例代码:
```csharp
private void SetRoundedPictureBox(PictureBox pictureBox, int radius)
{
Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (var brush = new TextureBrush(pictureBox.Image))
{
using (var path = GetRoundedPath(pictureBox.ClientRectangle, radius))
{
graphics.FillPath(brush, path);
}
}
pictureBox.Image = bitmap;
}
private GraphicsPath GetRoundedPath(Rectangle rectangle, int radius)
{
GraphicsPath path = new GraphicsPath();
path.StartFigure();
path.AddArc(rectangle.X, rectangle.Y, radius * 2, radius * 2, 180, 90);
path.AddLine(rectangle.X + radius, rectangle.Y, rectangle.Right - radius * 2, rectangle.Y);
path.AddArc(rectangle.X + rectangle.Width - radius * 2, rectangle.Y, radius * 2, radius * 2, 270, 90);
path.AddLine(rectangle.Right, rectangle.Y + radius * 2, rectangle.Right, rectangle.Y + rectangle.Height - radius * 2);
path.AddArc(rectangle.X + rectangle.Width - radius * 2, rectangle.Y + rectangle.Height - radius * 2, radius * 2, radius * 2, 0, 90);
path.AddLine(rectangle.Right - radius * 2, rectangle.Bottom, rectangle.X + radius * 2, rectangle.Bottom);
path.AddArc(rectangle.X, rectangle.Y + rectangle.Height - radius * 2, radius * 2, radius * 2, 90, 90);
path.AddLine(rectangle.X, rectangle.Bottom - radius * 2, rectangle.X, rectangle.Y + radius * 2);
path.CloseFigure();
return path;
}
```
其中,SetRoundedPictureBox方法用于设置PictureBox控件的圆角,需要传入PictureBox控件和圆角半径两个参数。GetRoundedPath方法用于获取圆角路径,需要传入控件的矩形和圆角半径两个参数。
--相关问题--:
1. 如何在C#中设置图片大小?
2. 如何在C#中设置图片透明度?
3. 如何在C#
阅读全文