Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active February 19, 2024 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/db16de7a770e0ad02ce9a418a6bee38e to your computer and use it in GitHub Desktop.
Save conholdate-gists/db16de7a770e0ad02ce9a418a6bee38e to your computer and use it in GitHub Desktop.
Crop and Resize JPEG Image using C#

Learn how to crop and resize JPEG images using C#

The following topics shall be covered in this article:

  1. C# Image API to Crop and Resize JPEG
  2. Crop JPEG Images using C#
  3. Crop Image in Specific Size using C#
  4. Resize JPEG Images in C#
  5. Resize JPEG Images Proportionally in C#
// This code example demonstrates how to crop JPG image using crop by Rectangle approach.
// Load an existing image into an instance of RasterImage class
RasterImage rasterImage = (RasterImage)Image.Load(@"C:\Files\images\aspose_logo.jpg");
// Before cropping, the image should be cached for better performance
if (!rasterImage.IsCached)
{
rasterImage.CacheData();
}
// Create an instance of Rectangle class with desired size
Rectangle rectangle = new Rectangle(35, 35, 580, 240);
// Perform the crop operation on object of Rectangle class
rasterImage.Crop(rectangle);
// Save the cropped image
rasterImage.Save(@"C:\Files\images\CroppingByRectangle_out.jpg");
// This code example demonstrates how to crop JPG image using crop by shift approach.
// Load an existing image into an instance of RasterImage class
RasterImage rasterImage = (RasterImage)Image.Load(@"C:\Files\images\aspose_logo.jpg");
// Before cropping, the image should be cached for better performance
if (!rasterImage.IsCached)
{
rasterImage.CacheData();
}
// Define shift values for all four sides
int leftShift = 30;
int rightShift = 30;
int topShift = 30;
int bottomShift = 30;
// Based on the shift values, apply the cropping on image
// Crop method will shift the image bounds toward the center of image
rasterImage.Crop(leftShift, rightShift, topShift, bottomShift);
// Save the cropped image
rasterImage.Save(@"C:\Files\images\cropped_out.jpg");
// This code example demonstrates how to crop JPG image using crop by Rectangle approach.
// Load an existing image
Image image = Image.Load(@"C:\Files\images\aspose_logo.jpg");
// New width and height
image.Resize(300, 100);
// Save the resized image
image.Save(@"C:\Files\images\SimpleResizing_out.jpg");
// This code example demonstrates how to crop JPG image using crop by Rectangle approach.
// Load an existing image
Image image = Image.Load(@"C:\Files\images\sample.jpg");
// Width
int newWidth = image.Width / 2;
image.ResizeWidthProportionally(newWidth);
// Height
int newHeight = image.Height / 2;
image.ResizeHeightProportionally(newHeight);
// Define save options
JpegOptions saveOptions = new JpegOptions();
saveOptions.Quality = 100;
// Save the image
image.Save(@"C:\Files\images\sample_out.jpg", saveOptions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment