Export Images in Multi Threaded Environment

Export Images in Multi Threaded Environment

Aspose.Imaging for Java now supports converting images in multi threaded environment. Aspose.Imaging for Java ensure the optimized performance of operations during execution of code in multi-threaded environment. All imaging option classes (e.g. BmpOptions, TiffOptions, JpegOptions, etc.) in the Aspose.Imaging for Java now implement com.aspose.imaging.system.IDisposable interface. Therefore it is a must that developer properly dispose off the imaging options class object in case Source property is set. Following code snippet demonstrates the said functionality.

// Create temporary image.
java.io.File tmp = java.io.File.createTempFile("image", "tes");
// Delete the file. This statement should execute to make it sure that resource is properly disposed off.
tmp.deleteOnExit();
// Path & name of existing image.
String imageDataPath = tmp.getAbsolutePath();
// Create the stream of the existing image file.
java.io.InputStream fileStream = new java.io.FileInputStream(tmp);
try
{
// Create an instance of BMP image option class.
com.aspose.imaging.imageoptions.BmpOptions bmpOptions = new com.aspose.imaging.imageoptions.BmpOptions();
try
{
bmpOptions.setBitsPerPixel(32);
// Set the source property of the imaging option class object.
bmpOptions.setSource(new com.aspose.imaging.sources.StreamSource(fileStream));
// Do processing.
// Following is the sample processing on the image. Un-comment to use it.
// com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage)com.aspose.imaging.Image.create(bmpOptions, 10, 10);
// try
// {
// com.aspose.imaging.Color[] pixels = new com.aspose.imaging.Color[4];
// for (int i = 0; i < 4; ++i)
// {
// pixels[i] = com.aspose.imaging.Color.fromArgb(40, 30, 20, 10);
// }
// image.savePixels(new com.aspose.imaging.Rectangle(0, 0, 2, 2), pixels);
// image.save(imageDataPath);
//}
//finally
//{
// This statement is in the final block because in any case this statement should execute to make it sure that resource is properly closed.
// image.close();
//}
}
finally
{
// This statement is in the final block because in any case this statement should execute to make it sure that resource is properly closed.
bmpOptions.close();
}
}
finally
{
// This statement is in the final block because in any case this statement should execute to make it sure that resource is properly disposed off.
fileStream.close();
fileStream = null;
}

Synchronize Access to Source Stream

Aspose.Imaging now supports getSyncRoot property while working in multi-threaded environment. Developer can use this property to synchronize access to the source stream. Following code snippet demonstrates how the getSyncRoot property can be used.

// create new synchronized two-way stream
com.aspose.imaging.StreamContainer streamContainer = new com.aspose.imaging.StreamContainer(new java.io.ByteArrayInputStream(new byte[0]));
try
{
// check if the access to the stream source is synchronized.
synchronized (streamContainer.getSyncRoot())
{
// do work
// now access to streamContainer is synchronized
}
}
finally
{
streamContainer.dispose();
}