2009-11-18 24 views

Respuesta

18

Esta función parece hacer lo que quiera. También se puede modificar fácilmente para devolver un mapa de bits si es necesario. También tendrá que limpiar las imágenes que ya no sean útiles, etc .. Adaptado 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... 
+0

esto es perfecto, gracias! – cocoapriest

+0

hay una manera de suavizar los bordes aún más. compare # 5 y # 6 en esta imagen http://danbystrom.files.wordpress.com/2008/08/fluffysample.jpg –

+1

¿Tal vez intente cambiar el modo de suavizado en el método a 'SmoothingMode.HighQuality'? –

5

La forma más sencilla sería utilizar una máscara escalable con las esquinas redondeadas. Aplicar la máscara a la imagen y exportar la nueva imagen.

Here es un artículo de CodeProject que se ocupa precisamente de eso.

7

Utilizando el método Graphics.SetClip() es la mejor manera. Por ejemplo:

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

Esto literalmente crea una imagen en forma de un óvalo o círculo. Es bueno saber cómo se puede hacer esto, pero no lo que la gente suele decir con "esquinas redondeadas". Sin ajuste del radio de la esquina usando este método. – jk7

+3

Vamos, utiliza tu imaginación para pete sake. Puede hacer cualquier forma que desee con un GraphicsPath combinando arcos, líneas, polígonos y beziers. –

2

Todas las demás respuestas adolecen de un problema con una distorsión de 1 píxel a lo largo del borde izquierdo y superior. Este código corrige el problema al compensar -1 píxeles a la izquierda y arriba al agregar los arcos de la máscara.

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

que terminó la combinación de ambos https://stackoverflow.com/a/1759073 y https://stackoverflow.com/a/1759225 para conseguir mis imágenes redondeadas, como quería el fondo sea transparente. Pensé en compartirlo:

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