如何在c#/ winforms中将图像裁剪为圆形? [英] How to crop an image to a circle in c# / winforms?
问题描述
编辑:重复问题中给出的代码对我来说没有解决问题。
the code given in the "duplicate" question didn't solve the problem for me.
我遇到的主要问题是我可以只需使用CSS和radius,这很容易。
The main problem I'm having is that I can't simply use CSS and radius, which would be easy.
这是在winforms页面/项目中正在加载的图像。我必须尝试将正方形/矩形图像制作成圆形。我尝试了以下2种方法(结果将在每种方法下方发布):
It's an image being loaded in, in a winforms page/project. I have to try and make a square/rectangle image into a circle. I've tried following 2 methods (results will be posted beneath each):
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;
}
}
调用代码:
Image StartImage = Image.FromFile(medwImg);
Image RoundedImage = btn.RoundCorners(StartImage, 100, Color.Transparent);
btn.NormalImage = RoundedImage;
结果:
如果将半径值更改为145,则可以看到以下结果:
If you change the radius value to 145 you can see this result:
< a href = https://i.stack.imgur.com/Cqqyw.png rel = nofollow noreferrer>
如您所见,也不好。
这是我的第二种方法:
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;
}
调用代码:
Image StartImage = Image.FromFile(medwImg);
Image RoundedImage = btn.CropToCircle(StartImage, Color.FromArgb(0, 101, 167));
btn.NormalImage = RoundedImage;
结果如下:
这看起来更好,但仍然不是一个完整的圆圈。
This looks better, but still isn't a nice full circle.
我的最佳猜测是第二种方法最接近一种好的解决方案,但是我不知道下一步该怎么做才能从中创建一个完整的圆
My best guess is the 2nd method is closest by to a good solution, but I don't know what to do next in order for a full circle to be created from the source image.
推荐答案
我已经使用以下方法找到了问题的答案:
I've found an answer to my problem, using following method:
// 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;
}
}
代码调用方法:
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;
现在我得到了漂亮的圆形图像(结果):
Now I get beautiful round images (result):
这篇关于如何在c#/ winforms中将图像裁剪为圆形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!