How to crop image to circle in C#

How to crop image to circle


Programming Language: C#.Net

IDE: Visual Studio 2015

Source code: Source code




Step 1: Open Visual Studio 2015. You have to create new project and choose Windows Forms Application.

Step 2: Create a user interface by adding two(2) picture boxes and 3 buttons.


Step 3: The next step is to add a class, then write in a code the codes below.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public class ImageHelper
{
public Image CropImage(Image img)
    {
        int x = img.Width/2;
        int y = img.Height/2;
        int r = Math.Min(x, y);

        Bitmap tmp = null;
        tmp = new Bitmap(2 * r, 2 * r);
        using (Graphics g = Graphics.FromImage(tmp))
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.TranslateTransform(tmp.Width / 2, tmp.Height / 2);
            GraphicsPath gp = new GraphicsPath();
            gp.AddEllipse(0 - r, 0 - r, 2 * r, 2 * r);
            Region rg = new Region(gp);
            g.SetClip(rg, CombineMode.Replace);
            Bitmap bmp = new Bitmap(img);
            g.DrawImage(bmp, new Rectangle(-r, -r, 2 * r, 2 * r), new Rectangle(x - r, y - r, 2 * r, 2 * r), GraphicsUnit.Pixel);

        }


            return tmp;
    }
    public void SaveImage(Image img,string path,ImageFormat imageFormat)
    {
        img.Save(path, imageFormat);
    }

}







Step 4: For Form1, add the codes specified for each button:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace CropImageToCircle
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //Browse Image
        private void button1_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();
            ofd.Filter = "Choose Image(*.jpg;,*.jpeg;,*.png;)|*.jpg;,*.jpeg;,*.png;";
            if(ofd.ShowDialog()== DialogResult.OK)
            {
                pictureBox1.Image =Image.FromFile( ofd.FileName);
            }
        }

        //CropToCircle
        private void button2_Click(object sender, EventArgs e)
        {
           
            var imageHelper = new ImageHelper();
           pictureBox2.Image= imageHelper.CropImage(pictureBox1.Image);

        }

        //Save Image
        private void button3_Click(object sender, EventArgs e)
        {
            var imageHelper = new ImageHelper();
            var sfd = new SaveFileDialog();
            sfd.FileName = "*";
            sfd.DefaultExt = "png";
            sfd.Filter = "Png Image (.png)|*.png ";
            sfd.ValidateNames = true;
            sfd.FilterIndex = 1;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                imageHelper.SaveImage(pictureBox2.Image,sfd.FileName,ImageFormat.Png);
            }
            
        }
    }
}








Step 7: The final step is to run the program.





You can download the source code at Github


Thank You!





Comments

Post a Comment

Popular posts from this blog