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);
Why use JDeli?
JDeli offers a range of advantages over ImageIO and alternatives, including:
- prevents heap related JVM crashes
- implements unsupported image formats
- reduce output file size
- improve read/write performance
- supports threading
- superior image scaling algorithms
This article is part of our series on reading and writing image files in Java.