如何在 c#/winforms 中将图像裁剪为圆形?

Tem*_*ght 6 c# graphics crop winforms

编辑:“重复”问题中给出的代码并没有为我解决问题。

我遇到的主要问题是我不能简单地使用 CSS 和半径,这很容易。

它是在 winforms 页面/项目中加载的图像。我必须尝试将正方形/矩形图像变成圆形。我尝试了以下 2 种方法(结果将在每种方法下方发布):

public Image RoundCorners(Image StartImage, int CornerRadius, Color BackgroundColor)
{
    CornerRadius *= 2;
    Bitmap RoundedImage = new Bitmap(StartImage.Width, StartImage.Height);

    using (Graphics g = Graphics.FromImage(RoundedImage))
    {
        g.Clear(BackgroundColor);
        g.SmoothingMode = SmoothingMode.AntiAlias;

        Brush brush = new TextureBrush(StartImage);

        GraphicsPath gp = new GraphicsPath();
        gp.AddArc(0, 0, CornerRadius, CornerRadius, 180, 90);
        gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0, CornerRadius, CornerRadius, 270, 90);
        gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
        gp.AddArc(0, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
        g.FillPath(brush, gp);

        return RoundedImage;
    }
}
Run Code Online (Sandbox Code Playgroud)

被调用的代码:

Image StartImage = Image.FromFile(medwImg);
Image RoundedImage = btn.RoundCorners(StartImage, 100, Color.Transparent);
btn.NormalImage = RoundedImage;
Run Code Online (Sandbox Code Playgroud)

结果:

圆角

如果将半径值更改为 145,您可以看到以下结果:

圆角145

如您所见,也不好。

这是我的第二种方法:

public Image CropToCircle(Image srcImage, Color backGround)
{
    Image dstImage = new Bitmap(srcImage.Width, srcImage.Height, srcImage.PixelFormat);
    Graphics g = Graphics.FromImage(dstImage);

    using (Brush br = new SolidBrush(backGround))
    {
        g.FillRectangle(br, 0, 0, dstImage.Width, dstImage.Height);
    }

    GraphicsPath path = new GraphicsPath();
    path.AddEllipse(0, 0, dstImage.Width, dstImage.Height);
    g.SetClip(path);
    g.DrawImage(srcImage, 0, 0);        

    return dstImage;
}
Run Code Online (Sandbox Code Playgroud)

被调用的代码:

Image StartImage = Image.FromFile(medwImg);
Image RoundedImage = btn.CropToCircle(StartImage, Color.FromArgb(0, 101, 167));
btn.NormalImage = RoundedImage;
Run Code Online (Sandbox Code Playgroud)

结果如下:

裁剪到圆

这看起来更好,但仍然不是一个很好的完整圆圈。

我最好的猜测是第二种方法最接近一个好的解决方案,但我不知道下一步要做什么才能从源图像创建一个完整的圆圈。

Tem*_*ght 13

我找到了我的问题的答案,使用以下方法:

// makes nice round ellipse/circle images from rectangle images
public Image ClipToCircle(Image srcImage, PointF center, float radius, Color backGround)
{
    Image dstImage = new Bitmap(srcImage.Width, srcImage.Height, srcImage.PixelFormat);

    using (Graphics g = Graphics.FromImage(dstImage))
    {
        RectangleF r = new RectangleF(center.X - radius, center.Y - radius,
                                                 radius * 2, radius * 2);

        // enables smoothing of the edge of the circle (less pixelated)
        g.SmoothingMode = SmoothingMode.AntiAlias;

        // fills background color
        using (Brush br = new SolidBrush(backGround))
        {
            g.FillRectangle(br, 0, 0, dstImage.Width, dstImage.Height);
        }

        // adds the new ellipse & draws the image again 
        GraphicsPath path = new GraphicsPath();
        path.AddEllipse(r);
        g.SetClip(path);
        g.DrawImage(srcImage, 0, 0);

        return dstImage;
    }
}
Run Code Online (Sandbox Code Playgroud)

调用方法的代码:

Image StartImage = Image.FromFile(medwImg);
Image RoundedImage = btn.ClipToCircle(StartImage, new PointF(StartImage.Width/2, StartImage.Height/2), StartImage.Width/2, Color.FromArgb(0, 101, 167));
btn.NormalImage = RoundedImage;
Run Code Online (Sandbox Code Playgroud)

现在我得到漂亮的圆形图像(结果):

圆形图像成功

  • 您应该在 `path` 上调用 `Dispose`(或使用 `using`),因为 `GraphicsPath` 实现了 `IDisposable`,因为它包含非托管 GDI 资源。 (6认同)