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.
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.
You can easily replace ImageIO by just changing ImageIO.read to JDeli.read:
//Read Image (can also be OutputStream or byte array) BufferedImage image = JDeli.read(wmfFile);
Or you can use the JDeli WmfDecoder directly:
WmfDecoder decoder = new WmfDecoder(); BufferedImage decodedImage = decoder.read(wmfByteData);
WmfDecoder decoder = new WmfDecoder(); BufferedImage decodedImage = decoder.read(wmfFile);
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.