Cropping pictures is a common operation in our life. So how do you crop images using C#?

First of all, we need to understand that cropping an image is actually cutting out a certain rectangular area in the original image. We need to determine the coordinates (x, y) of this rectangle in the image, as well as the width and height of the rectangle.

In C# we crop the image as follows:

  1. Loads an Image from a file as an Image object
  2. Creates a new empty image and specifies the size of the image after cropping
  3. Draw the target clipping area image from the Graphics object to the empty image created in the second step
  4. complete

The code is as follows:

OriginImage = image.fromfile (@"C:\Users\swsk33\Pictures\Saved Pictures\example.png"); Rectangle cropRegion = new Rectangle(10, 10, 50); // Rectangle cropRegion = new Rectangle(10, 10, 50, 50); // Rectangle cropRegion = new Rectangle(10, 10, 50, 50); Bitmap result = new Bitmap(cropregion.width, cropregion.height); // Create a blank canvas with the size of the clipped area. // Create the Graphics object and specify that the result (destination image canvas) is to draw the image Graphics = graphics.fromimage (result); / / using Graphics object the area defined by the original image cut down and fill into the newly created blank canvas Graphics. The DrawImage (originImage, new Rectangle (0, 0, cropRegion Width, cropRegion.Height), cropRegion, GraphicsUnit.Pixel); Result. Save(@"E:\ transfer \a.png", imageformat.png);Copy the code

The above DrawImage method has four parameters, which are explained further here:

  • The first parameter represents the original (image to crop) object
  • The second argument is a rectangular object that represents where the clipped portion of the image is placed on the target image canvas. Here is the cropped image, there is no special requirement that the target canvas is used exclusively for the cropped image. So we specify the size of the clipped area at the top left vertex of the target canvas (0, 0).
  • The third parameter is also a rectangle object, which represents the clipping region of the original image. This is the determination of the clipping region of the original image mentioned above
  • Represents the unit of measurement for clipping. The default is pixels

That’s the end of the crop! The emphasis is on understanding the parameters that need to be determined for image cropping.

For convenience, a function is written here. Just pass in the Image object of the original Image and the clipping region rectangle object to get the clipped Image object:

/ / / < summary > / / / cutting picture / / / < summary > / / / < param name = "originImage" > the original picture < param > / / / < param name = "region" > cut square area < param > Public static Image CropImage(Image originImage, Rectangle region) { Bitmap result = new Bitmap(region.Width, region.Height); Graphics graphics = Graphics.FromImage(result); graphics.DrawImage(originImage, new Rectangle(0, 0, region.Width, region.Height), region, GraphicsUnit.Pixel); return result; }Copy the code