Comment créer une image avec des angles arrondis en C #?

J’aimerais créer une image (à partir d’une autre) avec des angles arrondis avec GDI +. Quelle est la meilleure façon de faire cela?

PS: ce n’est pas pour le Web, je ne peux donc pas utiliser de CSS client

Cette fonction semble faire ce que vous voulez. Il peut également être facilement modifié pour renvoyer un bitmap si nécessaire. Vous devrez également nettoyer les images dont vous ne voulez plus, etc. Adapté de: 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... 

L’utilisation de la méthode Graphics.SetClip () est la meilleure solution. Par exemple:

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

La méthode la plus simple consiste à utiliser un masque évolutif aux angles arrondis. Appliquez le masque à l’image et exportez la nouvelle image.

Voici un article de CodeProject traitant précisément de cela.

J’ai fini par combiner les https://stackoverflow.com/a/1759073 et https://stackoverflow.com/a/1759225 pour obtenir des images arrondies, car je voulais que l’arrière-plan soit transparent. Je pensais le partager:

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

Toutes les autres réponses souffrent d’un problème de distorsion de 1 pixel le long des bordures gauche et supérieure. Ce code corrige le problème en décalant -1 pixels à gauche et en haut lors de l’ajout d’arcs pour le masque.

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