I'd like to create image (from another one) with rounded corners with GDI+. What's the best way to do this?
PS: it's not for web, so I cannot make use of client CSS
This function seems to do what you want. It can also easily be modified to return a Bitmap if needed. You'll also need to clean up any images you no longer want, etc.. Adapted from: 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...
I ended up combining both https://stackoverflow.com/a/1759073 and https://stackoverflow.com/a/1759225 to get my rounded images, as I wanted the background to be transparent. Thought I'd share it:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With