Published on: 23 Jul, 2013

How to crop image in ASP.Net C#?

Tumblr Digg Google Plus

In this tutorial, we will show you a C# function that will crop an image according to the dimension width and height you specify in the function parameters.

The following function will accept 4 paramaters

  1. Width: This will be the width of cropped image.
  2. Height: This will be the height of cropped image.
  3. Source Image File Path: This will be the full path of the source image file.
  4. Save Cropped Image File Path: This will be the full path of the save cropped image file.
public static void CropImage(int Width, int Height, string sourceFilePath, string saveFilePath) {
    // variable for percentage resize 
    float percentageResize = 0;
    float percentageResizeW = 0;
    float percentageResizeH = 0;

    // variables for the dimension of source and cropped image 
    int sourceX = 0;
    int sourceY = 0;
    int destX = 0;
    int destY = 0;

    // Create a bitmap object file from source file 
    Bitmap sourceImage = new Bitmap(sourceFilePath);

    // Set the source dimension to the variables 
    int sourceWidth = sourceImage.Width;
    int sourceHeight = sourceImage.Height;

    // Calculate the percentage resize 
    percentageResizeW = ((float)Width / (float)sourceWidth);
    percentageResizeH = ((float)Height / (float)sourceHeight);

    // Checking the resize percentage 
    if (percentageResizeH < percentageResizeW) {
        percentageResize = percentageResizeW;
        destY = System.Convert.ToInt16((Height - (sourceHeight * percentageResize)) / 2);
    } else {
        percentageResize = percentageResizeH;
        destX = System.Convert.ToInt16((Width - (sourceWidth * percentageResize)) / 2);
    }

    // Set the new cropped percentage image
    int destWidth = (int)Math.Round(sourceWidth * percentageResize);
    int destHeight = (int)Math.Round(sourceHeight * percentageResize);

    // Create the image object 
    using (Bitmap objBitmap = new Bitmap(Width, Height)) {
        objBitmap.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
        using (Graphics objGraphics = Graphics.FromImage(objBitmap)) {
            // Set the graphic format for better result cropping 
            objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            objGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            objGraphics.DrawImage(sourceImage, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);

            // Save the file path, note we use png format to support png file 
            objBitmap.Save(saveFilePath, ImageFormat.Png);
        }
    }
}

How to use this function

	CropImage(100, 100, "c://destinationimage.jpg", "c://newcroppedimage.jpg");

You may interested with our other article about How to resize image in ASP.Net C#.

Comments

Nitin
11 Sep, 2019
showing Out of memory exception
Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.