2009-11-18 38 views

Trả lời

18

Chức năng này dường như làm những gì bạn muốn. Nó cũng có thể dễ dàng được sửa đổi để trả về một Bitmap nếu cần thiết. Bạn cũng sẽ cần phải dọn dẹp bất kỳ hình ảnh mà bạn không còn muốn, vv .. Trích từ: 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

điều này là hoàn hảo, cảm ơn! – cocoapriest

+0

là có cách để làm mịn các cạnh hơn nữa. so sánh # 5 & # 6 trong hình ảnh này http://danbystrom.files.wordpress.com/2008/08/fluffysample.jpg –

+1

Có thể thử thay đổi chế độ làm mịn trong phương thức thành 'SmoothingMode.HighQuality'? –

5

Cách đơn giản nhất là nên sử dụng một mặt nạ khả năng mở rộng với các góc tròn. Áp dụng mặt nạ cho hình ảnh và xuất hình ảnh mới.

Here là một bài viết về CodeProject giải quyết chính xác điều đó.

7

Sử dụng phương pháp Graphics.SetClip() là cách tốt nhất. Ví dụ:

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

Điều này theo nghĩa đen tạo ra một hình ảnh trong hình dạng của một hình bầu dục hoặc hình tròn. Tốt để biết làm thế nào điều này có thể được thực hiện, nhưng không phải những gì mọi người thường có nghĩa là "góc tròn". Không điều chỉnh bán kính góc bằng phương pháp này. – jk7

+3

Thôi nào, hãy sử dụng trí tưởng tượng của bạn vì lợi ích. Bạn có thể tạo ra bất kỳ hình dạng nào bạn muốn với một GraphicsPath bằng cách kết hợp các cung, đường, đa giác và beziers. –

2

Tất cả các câu trả lời khác đều gặp vấn đề với méo 1 pixel dọc theo đường viền trái và trên. Mã này khắc phục sự cố bằng cách bù trừ -1 pixel ở bên trái và trên cùng khi thêm vòng cung cho mặt nạ.

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

tôi đã kết thúc kết hợp cả hai https://stackoverflow.com/a/1759073https://stackoverflow.com/a/1759225 để có được hình ảnh tròn của tôi, như tôi muốn nền phải minh bạch. Tôi nghĩ rằng tôi muốn chia sẻ nó:

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; 
} 
Các vấn đề liên quan