2009-11-18 15 views

risposta

18

Questa funzione sembra fare ciò che vuoi. Può anche essere facilmente modificato per restituire una Bitmap se necessario. Avrete anche bisogno di ripulire eventuali immagini che si desidera non è più, ecc .. Adattato da: 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... 
+0

questo è perfetto, grazie! – cocoapriest

+0

c'è un modo per smussare ulteriormente i bordi. confronta # 5 e # 6 in questa immagine http://danbystrom.files.wordpress.com/2008/08/fluffysample.jpg –

+1

Forse prova a cambiare la modalità di smoothing nel metodo a 'SmoothingMode.HighQuality'? –

5

Il modo più semplice sarebbe quella di utilizzare una maschera scalabile con gli angoli arrotondati. Applica la maschera all'immagine ed esporta la nuova immagine.

Here è un articolo CodeProject che si occupa proprio di questo.

7

Utilizzando il metodo Graphics.SetClip() è il modo migliore. Ad esempio:

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

Questo crea letteralmente un'immagine a forma di ovale o cerchio. Buono a sapersi come si può fare, ma non quello che le persone in genere intendono per "angoli arrotondati". Nessuna regolazione del raggio dell'angolo con questo metodo. – jk7

+3

Andiamo, usa la tua immaginazione per il bene di Dio. Puoi creare qualsiasi forma con un GraphicsPath combinando archi, linee, poligoni e beziers. –

2

Tutte le altre risposte presentano un problema con una distorsione di 1 pixel lungo il margine sinistro e superiore. Questo codice risolve il problema annullando -1 pixel a sinistra e in alto quando si aggiungono gli archi per la maschera.

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

ho finito per combinare entrambe https://stackoverflow.com/a/1759073 e https://stackoverflow.com/a/1759225 per ottenere le mie immagini arrotondati, come ho voluto lo sfondo di essere trasparenti. Ho pensato di condividerlo:

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