【发布时间】:2010-12-18 01:01:34
【问题描述】:
我想使用 GDI+ 创建带有圆角的图像(来自另一个图像)。最好的方法是什么?
PS:它不是用于网络的,所以我不能使用客户端 CSS
【问题讨论】:
标签: c# .net image rounded-corners
我想使用 GDI+ 创建带有圆角的图像(来自另一个图像)。最好的方法是什么?
PS:它不是用于网络的,所以我不能使用客户端 CSS
【问题讨论】:
标签: c# .net image rounded-corners
最直接的方法是使用带圆角的可缩放蒙版。将蒙版应用于图像并导出新图像。
Here 是 CodeProject 的一篇文章,专门处理这个问题。
【讨论】:
这个功能似乎做你想做的事。如果需要,它也可以很容易地修改为返回位图。您还需要清理您不再需要的任何图像等。改编自: http://www.jigar.net/howdoi/viewhtmlcontent98.aspx
using System.Drawing;
using System.Drawing.Drawing2D;
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("YourImageFile.jpg");
Image RoundedImage = this.RoundCorners(StartImage, 25, Color.White);
//Use RoundedImage...
【讨论】:
SmoothingMode.HighQuality?
使用 Graphics.SetClip() 方法是最好的方法。例如:
public static Image OvalImage(Image img) {
Bitmap bmp = new Bitmap(img.Width, img.Height);
using (GraphicsPath gp = new GraphicsPath()) {
gp.AddEllipse(0, 0, img.Width, img.Height);
using (Graphics gr = Graphics.FromImage(bmp)) {
gr.SetClip(gp);
gr.DrawImage(img, Point.Empty);
}
}
return bmp;
}
【讨论】:
所有其他答案都存在沿左边框和上边框 1 像素失真的问题。此代码通过在为蒙版添加弧时在左侧和顶部偏移 -1 个像素来解决此问题。
public static 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.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
using(Brush brush = new TextureBrush(StartImage))
{
using(GraphicsPath gp = new GraphicsPath())
{
gp.AddArc(-1, -1, CornerRadius, CornerRadius, 180, 90);
gp.AddArc(0 + RoundedImage.Width - CornerRadius, -1, CornerRadius, CornerRadius, 270, 90);
gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
gp.AddArc(-1, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
g.FillPath(brush, gp);
}
}
return RoundedImage;
}
}
【讨论】:
我最终结合了https://stackoverflow.com/a/1759073 和https://stackoverflow.com/a/1759225 来获得圆形图像,因为我希望背景是透明的。想我会分享它:
private Image RoundCorners(Image image, int cornerRadius)
{
cornerRadius *= 2;
Bitmap roundedImage = new Bitmap(image.Width, image.Height);
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);
using (Graphics g = Graphics.FromImage(roundedImage))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.SetClip(gp);
g.DrawImage(image, Point.Empty);
}
return roundedImage;
}
【讨论】:
盘旋时边缘更平滑。
public static Image RoundCorners(Image StartImage, float 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.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
using(Brush brush = new TextureBrush(StartImage))
{
using(GraphicsPath gp = new GraphicsPath())
{
gp.AddArc(0, -1, CornerRadius, CornerRadius, 180, 90);
gp.AddArc(-1 + RoundedImage.Width - CornerRadius, -1, CornerRadius, CornerRadius, 270, 90);
gp.AddArc(-1 + RoundedImage.Width - CornerRadius, -1 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
gp.AddArc(0, -1 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
g.FillPath(brush, gp);
}
}
return RoundedImage;
}
}
【讨论】: