Daniel Warren Daniel is a Java Developer at IDRsolutions, who is currently working on our BuildVu product. He enjoys experimenting with different computer systems, cycling, and a variety of PC games.

How to read in WMF images in Java

1 min read

In this post I will give an overview of WMF image files and show you how to read them in Java. Java’s ImageIO class does not support WMF file types, so you will need to use an external library. I will demonstrate first using TwelveMonkeys, and secondly using the JDeli Image Library.

What is a WMF?

Windows Metafile (WMF for short) is an image file format developed by Microsoft, which has the capabilities for both vector and bitmap images. It is more commonly used for it’s vector capabilities which would show it’s likeness to a file format such as SVG. Though the format may not see as much use as some popular formats like PNG and JPEG, you can still come across it’s use, and there’s no downside to knowing how to interact with it in Java.

There are two ways to interact with WMF in Java that I will demonstrate: TwelveMonkeys and our own JDeli Library.

TwelveMonkeys ImageIO

By default ImageIO does not support reading in WMF files. If you use TwelveMonkeys ImageIO library, you can use the ImageIO syntax to read a WMF file to a BufferedImage. This syntax is like so:

BufferedImage image = ImageIO.read(wmfFile)

More information can be found here.

JDeli

Alternatively you can use JDeli to decode the WMF files, and your example code would be:

WmfDecoder decoder = new WmfDecoder();
BufferedImage decodedImage = decoder.read(wmfByteData);

or

WmfDecoder decoder = new WmfDecoder();
BufferedImage decodedImage = decoder.read(wmfFile);

Further documentation on JDeli can be found here or you can see the Javadoc here.

JDeli has the advantage of inherently coming with support for reading in the WMF format. In addition to being able to read from the WMF file, JDeli allows you to read into the buffered image directly from the image’s byte data.

I hope you found this useful. You can find more tutorials in our series on handling common image formats in Java, JDeli and other image libraries.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the Blog RSS.

Daniel Warren Daniel is a Java Developer at IDRsolutions, who is currently working on our BuildVu product. He enjoys experimenting with different computer systems, cycling, and a variety of PC games.

Hacking the NetBeans IDE – 4. Changing the way…

To help us prepare for our talks for our talks at Oracle CodeOne – “Writing Better Code (Faster) in the Apache NetBeans IDE ” and “Hacking...
Mark Stephens
1 min read

Leave a Reply

Your email address will not be published. Required fields are marked *