Rendering Windows Metafiles to Raster Graphics

Skip to end of metadata
Go to start of metadata
Windows Metafiles (WMF) is the Windows graphic format developed by Microsoft. WMF format originally introduced for Vector Images may also include Raster Graphics. WMF is the native Vector Format for Microsoft Office applications including Microsoft Word, Microsoft PowerPoint etc. On the other hand, the Raster Formats like JPEG, PNG are used to store photographs and graphics. This article covers the following:
  • Metafiles versus Raster Graphics
  • Rendering Windows Metafiles to other graphics formats like PNG, JPEG in Java applications.

Advantages of Using Metafiles

There are mainly two advantages of using Windows Metafiles over Raster Graphics.

  • The Windows Metafiles are normally Vector Graphics and can store an image in a considerably less space than those of the Raster Graphics. This is because of the reason that Raster Graphics need to store large amount of information about the bitmap.
  • When Windows Metafiles are enlarged (or zoomed out), the visual quality of the graphic is not compromised. Whereas, the equivalent Raster Graphics look pixilated when viewed in an enlarged mode although the Raster Graphics can be scaled down without losing the quality.

Some Reasons for converting WMF to Raster Format

Although Windows Metafiles have a number of advantages, yet there are good reasons to convert WMF files to Raster Graphics as listed below:

  • In order to display an image on the screen, WMF files issue function calls to the Windows GDI (Graphics Device Interface) layer. This means, Metafiles may include executable code which can lead to some unexpected behavior.
  • Since Windows Metafiles contain vector information in 16-bit RGB color space, these files cannot be used in the high-end workflow due to 16-bit RGB color space limitations.

Some Reasons for converting WMF to Raster Format

Rendering WMF files in Java Applications

There are no built-in Java classes to handle Metafiles in Java applications. Aspose.Metafiles for Java provides the Java API classes that can not only be used to load and play Metafiles, but also enable Java applications to render Metafiles to Raster Graphic formats in a number of ways. In this article, we will use the simplest approach to serve this purpose.

Aspose.Metafiles for Java provides WmfMetafile class to load and perform various operations against Windows Metafiles. In order to load a WMF file, WmfMetafile class needs to be instantiated as shown below:

Java

WmfMetafile wmf = new WmfMetafile(new FileInputStream(new File("d:\lion.wmf"))); 

After a WMF file has been loaded, createDefaultRendering method exposed by WmfMetafile object can be used to return a RenderedImage object through the following statement:

Java

RenderedImage image = wmf.createDefaultRendering(); 
 

Now, we can write the rendered image to the disk. We can achieve this goal in a simplest way by using the functionality provided by Java Advanced Imaging (JAI) classes. ImageIO class from JAI package exposes the static write method that can write the rendered image to the desired graphic format. The following statement will save the image rendered from WMF file through Aspose.Metafiles for Java in Portable Network Graphics (PNG) format:

Java
ImageIO.write(image, "png",new File("d:\\LionPNG.png"));
 

Complete Code Listing

A complete code for rendering a WMF file to PNG format in the form of a Java class is as follows:

Java
 
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.aspose.metafiles.*;
 
public class renderWMF {
 
/**
* @param args
*/
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    renderWMF();
  }
  public static void renderWMF()
  {
    try {
              //Instantiate a EmfMetafile/WmfMetafile object that represents a metafile
        WmfMetafile wmf = new WmfMetafile(new FileInputStream(new File("d:\\lion.wmf")));
                 
              //Creating RenderedImage object for the metafile
              RenderedImage image = wmf.createDefaultRendering();
                 
              //Write the image to disk in PNG format
              ImageIO.write(image, "png",new File("d:\\LionPNG.png"));
              } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
              } catch (MetafilesException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
              } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
              }
   }
}

 

Conclusion

Despite the advantages of WMF files, we cannot use these files when developing enterprise level Java applications and we will have to render them to Raster Graphic formats. Due to unavailability of built-in support for handling WMF files in Java packages, Aspose.Metafiles for Java and JAI classes can be used to load as well as render to the desired graphic format with a few lines of code.

Related Sections

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.