Avoid Performance Degradation when Drawing over Compressed Images
Avoid Performance Degradation when Drawing over Compressed Images
There are times when you want to perform extremely extensive graphic operations on a compressed image. When Aspose.PSD has to compress and decompress images on the fly, performance degradation may occur. Drawing over compressed images may also carry a performance penalty.
Solution
To avoid performance degradation, we recommend that you convert the image to an uncompressed, or raw, format before performing graphic operations.
Using File Path
In the example that follows, a PSD image is converted to raw format (no compression) and saved to disk. The uncompressed image is then loaded back before graphic operations are performed on it. The same technique applies for BMP and GIF files.
String dataDir = Utils.getDataDir(UncompressedImageUsingFile.class) + "PSD/"; | |
// Load a PSD file as an image and cast it into PsdImage | |
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "layers.psd")) { | |
PsdOptions saveOptions = new PsdOptions(); | |
saveOptions.setCompressionMethod(CompressionMethod.Raw); | |
psdImage.save(dataDir + "uncompressed_out.psd", saveOptions); | |
// Now reopen the newly created image. | |
PsdImage img = (PsdImage) Image.load(dataDir + "uncompressed_out.psd"); | |
Graphics graphics = new Graphics(img); | |
// Perform graphics operations. | |
} |
Using a Stream Object
The following code snippet shows you how to PSD image is converted to raw format (no compression) and saved to disk using Stream.
String dataDir = Utils.getDataDir(UncompressedImageStreamObject.class) + "PSD/"; | |
ByteArrayOutputStream ms = new ByteArrayOutputStream(); | |
// Load a PSD file as an image and cast it into PsdImage | |
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "layers.psd")) { | |
PsdOptions saveOptions = new PsdOptions(); | |
saveOptions.setCompressionMethod(CompressionMethod.Raw); | |
psdImage.save(ms, saveOptions); | |
// Now reopen the newly created image. But first seek to the beginning of stream since after saving seek is at the end now. | |
ms.reset(); | |
PsdImage img = (PsdImage) Image.load(new ByteArrayInputStream(ms.toByteArray())); | |
Graphics graphics = new Graphics(psdImage); | |
// Perform graphics operations. | |
} |