如何在C#中创建带圆角的图像?

我想用GDI +创建带圆角的图像(来自另一个)。 最好的方法是什么?

PS:它不适用于网络,因此我无法使用客户端CSS

这个function似乎做你想要的。 如果需要,还可以轻松修改它以返回位图。 您还需要清理您不再需要的任何图像等。改编自: 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... 

使用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; } 

最直接的方法是使用带圆角的可伸缩面罩。 将蒙版应用于图像并导出新图像。

这是CodeProject正好处理的文章。

我最终结合了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; } 

所有其他答案都遇到沿左边和上边界出现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; } }