在绘制的压缩图像上避免性能降级

在绘制的压缩图像上避免性能降级

有时候,您可能希望在压缩图像上执行非常复杂的图形操作。当 Aspose.PSD 需要在运行时压缩和解压图像时,性能可能会下降。在压缩图像上绘制也可能会导致性能损失。

解决方案

为了避免性能下降,我们建议在执行图形操作之前将图像转换为未压缩或原始格式。

使用文件路径

在接下来的示例中,一个 PSD 图像被转换为原始格式(无压缩)并保存到磁盘。然后在执行图形操作之前重新加载未压缩的图像。相同的技术适用于 BMP 和 GIF 文件。

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.
}

使用流对象

下面的代码片段展示了如何将 PSD 图像转换为原始格式(无压缩)并使用 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.
}