This short tutorial guides on how to crop image in C# by explaining the process where a sample bitmap is cropped. There are a number of options by which images can be cropped. To crop bitmap C# code demonstrates one of the method where sides are shifted with the user-defined values and the output image is saved as BMP however you can save it to any other image type like JPEG, PNG, etc.
Steps to Crop Image in C#
- Configure the development environment to add Aspose.Imaging using the NuGet package manager
- Load the source bitmap using the Image class object
- Cast the loaded image to RasterImage
- Cache the complete image into memory for better performance
- Crop the loaded bitmap by defining the top, bottom, left, and right side shifting towards the image center
- Use Save method to save the cropped image on the disk
To cut image C# based environment settings, namespace, classes, and methods are shared in these steps. You can load any type of image using the Image.Load() method and then cast it to RasterImage which contains options to crop images. We have used the shifting of sides method here where you have to define the values by which the sides of the loaded images will move inward towards the center of the image thus creating a cropped image.
Code to Crop Image in C#
These lines of code in C# crop image and save it on the disk by using the RasterImage.Save() method. Once the source image is loaded as RasterImage, you can perform many other operations as well before cropping or saving the resultant image like setting the background color, setting the interrupt monitor to get control over terminating the process prematurely if required, setting the transparent color, setting the horizontal and vertical resolution, etc. to name a few.
This crisp tutorial has guided us to cut images in C#. If you want to learn to resize these images, refer to the article on how to resize image in C#.