Hello readers, in this blog I would like to introduce you with new features included in Aspose.Imaging 19.10 especially support for controlled memory optimization techniques for loading images using API. The best thing about the Aspose team is that it releases both .NET and Java-based APIs simultaneously every month. This way both API user remains recurrent with changes being made in APIs every month.

In the following sections, I am going to give you a glance at the features that have been included in API.

Load Images with Memory Optimization

At times there is a requirement to load huge images and you don’t have enough RAM available on the machine. In such cases, OutOfMemory exception is thrown on loading images. We have introduced the support for controlled memory specification in this new API. Now, you can have memory controlled limitation for loading image by specifying buffer size and it will guarantee that memory usage will be in those bounds. So, if you set memory optimization and your machine RAM memory is not enough, the image related operation will begin to work using hard drive. It will guarantee that operation will be performed in such a way without out of memory exception but it will take much time than using RAM. The controlled memory optimization techniques support has been provided for images like JPEG, CMX, and PNG.

The following examples demonstrate the use of memory optimization while loading the CMX images using API.

Memory Optimization in C#
// Setting a memory limit of 10 megabytes for target loaded image
using (Image image = Image.Load("example.cmx", new LoadOptions() { BufferSizeHint = 10 }))
{
image.Save(
"output.png",
new PngOptions()
{
VectorRasterizationOptions =
new CmxRasterizationOptions { TextRenderingHint = TextRenderingHint.SingleBitPerPixel, SmoothingMode = SmoothingMode.AntiAlias, Positioning = PositioningTypes.DefinedByDocument}
});
}

Likewise, the following examples demonstrate the use of memory optimization while loading JPEG images using API.

// Setting a memory limit of 50 megabytes for target loaded image
using (Image image = Image.Load("inputFile.jpg", new LoadOptions() { BufferSizeHint = 50 }))
{
image.Save("outputFile_Baseline.jpg", new JpegOptions { CompressionType = JpegCompressionMode.Baseline, Quality = 100 });
image.Save("outputFile_Progressive.jpg", new JpegOptions { CompressionType = JpegCompressionMode.Progressive });
image.Save("outputFile_Lossless.jpg", new JpegOptions
{
ColorType = JpegCompressionColorMode.YCbCr,
CompressionType = JpegCompressionMode.Lossless,
BitsPerChannel = 4
});
image.Save("outputFile_JpegLs.jpg", new JpegOptions
{
ColorType = JpegCompressionColorMode.YCbCr,
CompressionType = JpegCompressionMode.JpegLs,
JpegLsInterleaveMode = JpegLsInterleaveMode.None,
JpegLsAllowedLossyError = 3,
JpegLsPreset = null
});
}
// Setting a memory limit of 50 megabytes for target created image
ImageOptionsBase createOptions = new JpegOptions { CompressionType = JpegCompressionMode.Progressive };
createOptions.BufferSizeHint = 50;
createOptions.Source = new FileCreateSource("createdFile.jpg", false);
using (var image = Image.Create(createOptions, 1000, 1000))
{
image.Save(); // save to same location
}

Memory Optimization in Java

The similar Java-based implementation of loading CMX images is the following:

The similar Java-based implementation of memory optimization for JPEG images is the following:

LoadOptions options = new LoadOptions();
options.setBufferSizeHint(50);
// Setting a memory limit of 50 megabytes for target loaded image
try (Image image = Image.load("inputFile.jpg", options))
{
JpegOptions jpegOptions = new JpegOptions();
jpegOptions.setCompressionType(JpegCompressionMode.Baseline);
jpegOptions.setQuality(100);
image.save("outputFile_Baseline.jpg", jpegOptions);
jpegOptions = new JpegOptions();
jpegOptions.setCompressionType(JpegCompressionMode.Progressive);
image.save("outputFile_Progressive.jpg", jpegOptions);
jpegOptions = new JpegOptions();
jpegOptions.setCompressionType(JpegCompressionMode.Lossless);
jpegOptions.setColorType(JpegCompressionColorMode.YCbCr);
jpegOptions.setBitsPerChannel((byte)4);
image.save("outputFile_Lossless.jpg", jpegOptions);
jpegOptions = new JpegOptions();
jpegOptions.setCompressionType(JpegCompressionMode.JpegLs);
jpegOptions.setColorType(JpegCompressionColorMode.YCbCr);
jpegOptions.setJpegLsInterleaveMode(JpegLsInterleaveMode.None);
jpegOptions.setJpegLsAllowedLossyError(3);
jpegOptions.setJpegLsPreset(null);
image.save("outputFile_JpegLs.jpg", jpegOptions);
}
// Setting a memory limit of 50 megabytes for target created image
try (JpegOptions createOptions = new JpegOptions())
{
createOptions.setCompressionType(JpegCompressionMode.Progressive);
createOptions.setBufferSizeHint(50);
createOptions.setSource(new FileCreateSource("createdFile.jpg", false));
try (Image image = Image.create(createOptions, 1000, 1000))
{
image.save(); // save to same location
}
}

Performance Improvements in API

As against previous API versions, we have tended to improve the API performance in many areas when performing different operations including:

  • Rendering to PDF has been improved
  • Issues related to SVG rendering have been addressed

Wait, there are many other features, enhancement, and bug fixes included in this release. Here you can get the detail!

When time allows you can check out API examples at Github, talk about this release and other API related issues in our forum.