Crop and Resize JPEG Images using C#

Crop and Resize JPEG Image using C#

JPEG or JPG is one of the most commonly used image formats to store images and photos. In certain cases, we might need to crop or rotate images for various reasons. In this article, we will learn how to crop and resize JPEG images using C#.

The following topics shall be covered in this article:

C# Imaging API to Crop and Resize JPEG

For cropping and resizing images, we will be using Aspose.Imaging for .NET API. It enables us to create, load, manipulate, convert, and compress images or photos programmatically. Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.Imaging

Crop JPEG Images using C#

We can easily crop any JPEG/JPG image from all sides towards the center of the image. For this purpose, the RasterImage class provides the Crop(Int32, Int32, Int32, Int32) method that crops the image with specified shift values. It takes left, right, top, and bottom shift values as arguments to crop the image boundaries. We can crop the images using the crop by shift approach by following the steps given below:

  1. Firstly, load the input JPG image using the RasterImage class.
  2. Next, cache the image using the CacheData method.
  3. After that, call the Crop() method with the shift values as arguments.
  4. Finally, call the Save() method with the output file path as an argument to save the cropped image file.

The following code sample shows how to crop a JPEG image using C#.

Crop JPEG Images using C#
Crop JPEG Images using C#.

Crop Images to Specific Size using C#

The RasterImage class also provides an overloaded Crop(Rectangle) method that crops a particular area of an image to a specified size. It takes an instance of the Rectangle class as an argument. We can crop images to a specified size by following the steps given below:

  1. Firstly, load the input JPG image using the RasterImage class.
  2. Next, cache the image using the CacheData method.
  3. Then, initialize the Rectangle class object with specified location and size.
  4. After that, call the Crop() method with the created Rectangle object as an argument.
  5. Finally, call the Save() method with the output file path as an argument to save the cropped image file.

The following code sample shows how to crop a JPEG image to a specific size using C#.

Crop Image in Specific Size using C#.
Crop Image to Specific Size using C#.

Resize JPEG Images in C#

The Image class provides the Resize() method to resize the images. We can resize JPEG images by following the steps given below:

  1. Firstly, load the input JPG image using the Image class.
  2. Next, call the Resize(int32, int32) method with the required width and height as arguments.
  3. Finally, call the Save() method with the output file path as an argument to save the resized image.

The following code sample shows how to resize a JPEG image using C#.

Resize JPEG Images in C#
Resize JPEG Images in C#.

Resize JPEG Images Proportionally in C#

We may get stretched images when we resize with fixed height and width. We can use proportional resizing to avoid stretched images. For this purpose, the Image class provides ResizeWidthProportionally(Int32) and ResizeHeightProportionally(Int32) methods. Please follow the steps given below to resize an image proportionally to width and height.

  1. Firstly, load the input JPG image using the Image class.
  2. Next, call the Image.ResizeWidthProportionally(Int32) method with new width as arguments.
  3. After that, call the Image.ResizeHeightProportionally(Int32) method with new height as arguments.
  4. Finally, call the Save() method with the output file path as an argument to save the resized image.

The following code sample shows how to resize a JPEG image proportionally using C#.

Get a Free License

Please try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, we have learned how to:

  • crop JPEG images in C#;
  • crop an image to a specific size;
  • resize JPEG images programmatically;
  • resize JPEG images proportionally.

Besides, you can learn more about Aspose.Imaging for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also