Babar Raza July 7, 2015one Comment

Save Image to BufferedImage, Crop & Watermark Metafiles & Convert Raster Images to Metafiles with Aspose.Imaging for Java 2.9.1

Save Image to BufferedImage, Crop & Watermark Metafiles & Convert Raster Images to Metafiles with Aspose.Imaging for Java 2.9.1

July 7, 2015
Share on FacebookTweet about this on TwitterShare on LinkedIn

Aspose.Imaging for Java logoWe are pleased to announce the July’s release of Aspose.Imaging for Java. This month’s release contains many useful improvements which will certainly ease the task of processing Metafiles with Aspose.Imaging for Java. Please refer to the release notes of Aspose.Imaging for Java 2.9.1 for a full list of bug fixes and improvements along with sample code snippets for newly added features. If you are planning to upgrade the API to the latest revision, we strongly suggest you to check the Public API Change section to know what has been changed in the public API since your current version.

While you are downloading the latest build, here is a look at just a few of the showcased features of this release.

Storing Image in BufferedImage

While working with images in Java applications, we may encounter scenarios where we need to save the images generated with Aspose.Imaging for Java in an instance of BufferedImage. This simple conversion is now available with the help of ImageExtensions.toJava factory method.

Aspose.Imaging for Java 2.9.1 has exposed two versions of the ImageExtensions.toJava method to suit most of the application requirements. The first revision of the aforesaid method can accept an instance of com.aspose.imaging.Image and returns an object of java.awt.image.BufferedImage. Whereas the other revision of the ImageExtensions.toJava method can also accept an instance of com.aspose.imaging.Rectangle as second parameter to return a part of image as an object of java.awt.image.BufferedImage.

Cropping Metafiles

This release has exposed two versions of the MetafileImage.crop method to perform the cropping on Metafiles such as EMF. Both revisions of the MetafileImage.crop method accept different types of parameters and perform different type of cropping as elaborated below:

  • Cropping by Shifts: The first overload of crop method can accept 4 integer values denoting Left, Right, Top & Bottom. Based on these four values, the crop method moves the image boundaries toward the center of the image while discarding the outer portion.
  • Cropping by Rectangle: The other overloaded version of the crop method can accept an instance of the com.aspose.imaging.Rectangle class to cut out any portion of an image by providing the desired boundaries to the Rectangle object.

For now the resultant cropped image can only be stored in raster image formats such as BMP, PNG, TIFF, GIF & JPEG. In case the result has to be stored back in Metafile format then the changes will not take effect. We have already planned to enhance this feature for Metafile formats as well, and hope to publish the enhancements with next release of the Aspose.Imaging for Java API.

Watermarking Metafiles

Aspose.Imaging for Java has exposed the getWatermarkDrawer method for all classes that represent Metafiles such as EmfMetafileImage, MetafileImage and WmfMetafileImage. The getWatermarkDrawer method returns an instance of java.awt.Graphics2D object which in turn allows to create custom watermarks for Metafiles.

The following code example demonstrates the usage of Aspose.Imaging for Java API to watermark EMF images and store the result in raster image format.

//Load an EMF image in an instance of EmfMetafileImage
EmfMetafileImage image = (EmfMetafileImage)Image.load(sourceFilePath);

//Create an instance of Graphics2D by calling EmfMetafileImage.getWatermarkDrawer
java.awt.Graphics2D drawer = image.getWatermarkDrawer();

//Creates an instance of Font and initialize it with font name, style and size
java.awt.Font font = new java.awt.Font("Times New Roman", FontStyle.Bold, 32);

//Set font to the instance of Graphics2D

//Set the Paint attribute for the Graphics2D context with an instance of Color
drawer.setPaint(new java.awt.Color(0,0,255, 127));

//Create an instance of Rectangle2D by getting the string bounds
java.awt.geom.Rectangle2D rect = font.getStringBounds("This is the custom", drawer.getFontRenderContext());

//Estimate the X & Y positions where watermark will render on image
float posx = (float)(image.getWidth() - rect.getWidth()) / 2;
float posy = (float)(image.getHeight() - rect.getHeight()) / 2;

//Draw watermark on the image
drawer.drawString("This is the custom", posx, posy);
drawer.drawString("watermark string!", posx, posy + 30);

//Save the result in raster image format, new PngOptions());

Converting a Raster Image to Metafile

This release has exposed the EmfMetafileImage.createEmfRecorderGraphics method that returns an instance of EmfRecorderGraphics2D that in turn can be used to call the drawImage method while specifying the instance of Image to be inserted in EMF image.

The following code demonstrates the usage of EmfMetafileImage.createEmfRecorderGraphics method to add a raster image to EMF file.

//Load the image to be inserted
Image image = Image.load(sourceFilePath);

//Store the DPI value
float dpi = 200f;

//Create an instance of Rectangle to store the dimensions of the destination image
java.awt.Rectangle rectange = new java.awt.Rectangle(0, 0, image.getWidth(), image.getHeight());

//Create an instance of Dimension to store the dimensions of the source image
java.awt.Dimension dimension = new java.awt.Dimension(image.getWidth(), image.getHeight());

//Create an instance of EmfRecorderGraphics2D
EmfRecorderGraphics2D emfRecorderGraphics = EmfMetafileImage.createEmfRecorderGraphics(rectange, dimension, dpi, dpi);

//Draw the source image starting from top left corner
emfRecorderGraphics.drawImage(image, 0, 0, null);

//Create an instance of EmfMetafileImage
EmfMetafileImage emfMetafileImage = emfRecorderGraphics.endRecording();

//Save the result;

Aspose.Imaging for Java Resources

The resources, you may need to accomplish your tasks:

Join the Conversation

1 Comment

Leave a comment

Posted inAspose.Imaging Product Family, Babar Raza

Related Articles