Making image editing tool in C#-Cropping images

In this article we will learn how to crop image in C#

Download Source Code
Crop Image online for free

Design form like the following:

In crop tab there are one two button one is ‘Make selection’ and other for ‘Ok’.

For cropping an image we will need to define region that is need to crop, In this article we will use rectangle for defining such area or region.

When we click on ‘MakeSelection’ button and after that  draw a rectangle on picturebox with the help of mouse.

Code for draw rectangle:

Declare some private variables for finding cursor location and define drawing objects

    int cropX;
    int cropY;
    int cropWidth;
    int cropHeight;
    int oCropX;
    int oCropY;
 
    public Pen cropPen;
    public DashStyle cropDashStyle = DashStyle.DashDot;

 

We will use ‘mousedown’ And ‘mousemove’ event for doing this. We will need to implement this code on mouse down event:

    if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Cursor = Cursors.Cross;
            cropX = e.X;
            cropY = e.Y;
            cropPen = new Pen(Color.Black, 1);
            cropPen.DashStyle = DashStyle.DashDotDot;
         }
 
      PictureBox1.Refresh();

In above code there are two variables are using one is ‘cropX’ for x position of the rectangle location and another is ‘cropY’ for y position. ‘cropPen’ is the object of pen class with ‘pensize’ and ‘pencolor’.

And write this code on mouse move event

if (PictureBox1.Image == null)
   {
     return;
   }
if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
       PictureBox1.Refresh();
       cropWidth = e.X - cropX;
       cropHeight = e.Y - cropY;
       PictureBox1.CreateGraphics().DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight);
      }

On mouse event we can find cursor position with the help of event arguments so we can easily determine rectangle’s width and height and use ‘drawrectangle’ method. On above code cropPen determines the color, size and other styles of rectangle, ‘cropx’ is x coordinate of upper left corner of rectangle, ‘y’ is y coordinate of upper left corner of rectangle and ‘cropWidth’, ‘cropHeight’ are width and height of rectangle respectively.

Code for crop:

Cursor = Cursors.Default;
 if (cropWidth < 1)
 {
  return;
 }
 Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
 //First we define a rectangle with the help of already calculated points
 Bitmap OriginalImage = new Bitmap(PictureBox1.Image, PictureBox1.Width, PictureBox1.Height);
  //Original image
 Bitmap _img = new Bitmap(cropWidth, cropHeight);
 // for cropinf image
 Graphics g = Graphics.FromImage(_img);
 // create graphics
 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
 g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
 //set image attributes
 g.DrawImage(OriginalImage, 0, 0, rect, GraphicsUnit.Pixel);
 PictureBox1.Image = _img;
 PictureBox1.Width = _img.Width;
 PictureBox1.Height = _img.Height;
 PictureBoxLocation();
 btnCrop.Enabled = false;

In the above code, I have mentioned comment. So I think now we can crop any image  easily with the help of this scenario.


Other related articles of this series:

Making image editing tool in C#

making image editing tool in C#- Resize an image

Making image editing tool in C#- Brightness of an image

Making image editing tool in C#- Rotations of an image


4 thoughts on “Making image editing tool in C#-Cropping images”

  1. Hi,

    Thanks very much for this article, This code solved one of my major issues in the project i am currently working on. Appreciate it.

  2. thanx…………..
    i like this artical very much…..
    this is much helpful to new designer…. 

  3. Hi friend,
             I am also using this cropping concept in my project .   Please post the code for asp.net.

Comments are closed.