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.Imaging 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 Png image is converted to another Png and saved to disk. The image is then loaded back before graphic operations are performed on it. The same technique applies for BMP and GIF files.

using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// First convert the image to raw PSD format.
using (PngImage pngImage = (PngImage)Image.Load(dataDir + "template.png"))
{
PngOptions saveOptions = new PngOptions();
pngImage.Save(dataDir + "result.png", saveOptions);
}
// Now reopen the newly created image.
using (PngImage pngImage = (PngImage)Image.Load(dataDir + "result.png"))
{
Graphics graphics = new Graphics(pngImage);
// Perform graphics operations.
}
File.Delete(dataDir + "result.png");

Using a Stream Object

The following code snippet shows you how to Png image is converted to another Png image and saved to disk using MemoryStream.

using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Create an instance of MemoryStream to hold the uncompressed image data.
using (MemoryStream stream = new MemoryStream())
{
// First convert the image to raw PSD format.
using (PngImage pngImage = (PngImage)Image.Load(dataDir + "template.png"))
{
PngOptions saveOptions = new PngOptions();
pngImage.Save(dataDir + "result.png", saveOptions);
}
// Now reopen the newly created image.
using (PngImage pngImage = (PngImage)Image.Load(dataDir + "result.png"))
{
Graphics graphics = new Graphics(pngImage);
// Perform graphics operations.
}
}
File.Delete(dataDir + "result.png");