Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active December 23, 2021 05:57
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 aspose-com-gists/f93a3760be357b8f0ccd2f984f236816 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/f93a3760be357b8f0ccd2f984f236816 to your computer and use it in GitHub Desktop.
Crop or Rotate a PSD file Programmatically with C#
String sourceFile = "sample.psd";
String destName = "Cropping-PSD_out.jpg";
// Load an existing image into an instance of RasterImage class
RasterImage rasterImage = (RasterImage)Image.Load(sourceFile);
// Cache the image for better performance
if (!rasterImage.IsCached)
{
rasterImage.CacheData();
}
// Create an instance of Rectangle class with desired size.
Rectangle rectangle = new Rectangle(20, 20, 20, 20);
// Perform the crop operation on object of Rectangle class
rasterImage.Crop(rectangle);
// Save the results to disk
rasterImage.Save(destName, new ImageOptions.JpegOptions());
// Load an existing image into an instance of RasterImage class
RasterImage rasterImage = (RasterImage)Image.Load("Test.psd");
// 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 = 10;
int bottomShift = 10;
// Based on the shift values, apply the cropping on image Crop method will shift the image bounds toward the center of image and Save the results to disk
rasterImage.Crop(leftShift, rightShift, topShift, bottomShift);
// Save output in JPEG,PSD or any other format.
rasterImage.Save("output.jpg", new ImageOptions.JpegOptions());
rasterImage.Save("output.psd", new ImageOptions.PsdOptions());
String sourceFile = "sample.psd";
String destName = "Rotate-PSD_out.jpg";
// Load input PSD image file
RasterImage image = (RasterImage)Image.Load(sourceFile);
// Before rotation, the image should be cached for better performance
if (!image.IsCached)
{
image.CacheData();
}
// Rotate the PSD image on 20 degree angle while keeping the image size proportional with red background color
image.Rotate(20f, true, Color.Red);
// Save the result to a new file
image.Save(destName, new ImageOptions.JpegOptions());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment