圧縮された画像上に描画する際のパフォーマンスの低下を回避する方法

圧縮された画像上に描画する際のパフォーマンスの低下を回避する方法

圧縮された画像上で非常に膨大なグラフィック操作を実行したい場合があります。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.
}

Streamオブジェクトを使用

以下のコードスニペットは、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.
}
}