Read the complete article on how to crop images programmatically in C#: https://blog.aspose.com/imaging/crop-images-in-csharp/
Created
September 20, 2022 06:32
-
-
Save aspose-com-gists/f0d22423bdd8384b4ac6dededac1e8cf to your computer and use it in GitHub Desktop.
Crop Images Programmatically in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load the image. | |
using (RasterImage rasterImage = (RasterImage)Image.Load("image.png")) | |
{ | |
// Before cropping, the image should be cached for better performance. | |
if (!rasterImage.IsCached) | |
{ | |
rasterImage.CacheData(); | |
} | |
// Create an instance of Rectangle class with desired size and crop the image. | |
Rectangle rectangle = new Rectangle(20, 20, 20, 20); | |
rasterImage.Crop(rectangle); | |
// Save cropped image. | |
rasterImage.Save("cropped.png"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load the image to be cropped. | |
using (RasterImage rasterImage = (RasterImage)Image.Load("image.png")) | |
{ | |
// Before cropping, the image should be cached for better performance. | |
if (!rasterImage.IsCached) | |
{ | |
rasterImage.CacheData(); | |
} | |
// Define shift values for all four sides. | |
int leftShift = 10; | |
int rightShift = 10; | |
int topShift = 50; | |
int bottomShift = 50; | |
// 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 cropped image. | |
rasterImage.Save("cropped.png"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment