Round Image Corner in C#

During my software development I had a requirement where I Round Image Corner in C#. There was a requirement that user will upload a photo and I need to save it with rounded corners and show it. User may not upload photo with rounded corner. So I decided to and save it on server as rounded corner image.

This post will guide you how to round image corners using c#.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Image StartImage = Image.FromFile(@"c:\Test\image1.png");
            Image RoundedImage = RoundCorners(StartImage, 90, Color.Transparent);

            RoundedImage.Save(@"c:\Test\image1_round.png");
        }

        public static Image RoundCorners(Image StartImage, int CornerRadius, Color BackgroundColor)
        {
            CornerRadius *= 2;
            Bitmap RoundedImage = new Bitmap(StartImage.Width, StartImage.Height);
            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;
        }
    }
}

 

Enjoy…!!!