当在压缩图像上绘制时避免性能下降

当在压缩图像上绘制时避免性能下降

有时您想要在压缩图像上执行非常复杂的图形操作。当Aspose.PSD需要动态压缩和解压图像时,性能下降可能会发生。在压缩图像上绘制也可能会带来性能惩罚。

解决方案

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

使用文件路径

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

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Load a PSD file as an image and cast it into PsdImage
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "layers.psd"))
{
PsdOptions saveOptions = new PsdOptions();
saveOptions.CompressionMethod = CompressionMethod.Raw;
psdImage.Save(dataDir + "uncompressed_out.psd", saveOptions);
}
// Now reopen the newly created image.
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "uncompressed_out.psd"))
{
Graphics graphics = new Graphics(psdImage);
// Perform graphics operations.
}

使用流对象

下面的代码片段向您展示如何将PSD图像转换为原始格式(无压缩)并使用MemoryStream保存到磁盘。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
{
// Load a PSD file as an image and cast it into PsdImage
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "layers.psd"))
{
PsdOptions saveOptions = new PsdOptions();
saveOptions.CompressionMethod = CompressionMethod.Raw;
psdImage.Save(stream, saveOptions);
}
// Now reopen the newly created image. But first seek to the beginning of stream since after saving seek is at the end now.
stream.Seek(0, System.IO.SeekOrigin.Begin);
using (PsdImage psdImage = (PsdImage)Image.Load(stream))
{
Graphics graphics = new Graphics(psdImage);
// Perform graphics operations.
}
}