How can I convert one WMF file to PNG/BMP/JPG format with custom output resolution?
Example: Take WMF file and outputs PNG file with 2000x2000 px.
Thanks in advance.
WMF files contain both vector graphics and raster components at the same time. It includes sort of programming commands which enables the creation of lines, circles, and rectangles on the viewing applications. This 16-bit image format is portable between applications.
You can open WMF graphics with various image-editing and -viewing applications. Some programs include the Microsoft Office suite of applications (Windows and macOS), Adobe Illustrator (Windows and macOS), Inkscape (multiplatform), and XnViewMP (multiplatform).
Windows Metafile (WMF) is an image file format originally designed for Microsoft Windows in the 1990s. The original Windows Metafile format was not device-independent (though could be made more so with placement headers) and may contain both vector graphics and bitmap components.
The Windows Metafile (WMF and EMF) formats are vector formats that may or may not also contain a raster image. EMF/WMF Vector files can be read and written as raster images in the Imaging Pro toolkits. They can be read and written as vector images in the Document and Medical Imaging toolkits.
You can use the excelent Batik ( http://xmlgraphics.apache.org/batik/ ) lib to achieve this. But you will need to follow this steps:
WMF >> SVG >> JPG
Here is a discussion on coderanch about it: http://www.coderanch.com/t/422868/java/java/converting-WMF-Windows-Meta-File
WMF, EMF and EMF+ are integral parts of slideshows, therefore I've developed a renderer for those as part of Apache POI. As for the upcoming POI-4.1.2 this is still work in progress - if you have any rendering issues, please upload your file in a new bug report in our bugzilla.
The main description can be found in the POI documentation.
Here is an excerpt:
For file system access, you need to save your slideshow/WMF/EMF/EMF+ first to disc and then call PPTX2PNG.main()
with the corresponding parameters.
for stdin
access, you need to redirect System.in
before:
/* the file content */
InputStream is = ...;
/* Save and set System.in */
InputStream oldIn = System.in;
try {
System.setIn(is);
String[] args = {
"-format", "png", // png,gif,jpg,svg or null for test
"-outdir", new File("out/").getCanonicalPath(),
"-outfile", "export.png",
"-fixside", "long",
"-scale", "800",
"-ignoreParse",
"stdin"
};
PPTX2PNG.main(args);
} finally {
System.setIn(oldIn);
}
File f = samples.getFile("santa.wmf");
try (FileInputStream fis = new FileInputStream(f)) {
// for WMF
HwmfPicture wmf = new HwmfPicture(fis);
// for EMF / EMF+
HemfPicture emf = new HemfPicture(fis);
Dimension dim = wmf.getSize();
int width = Units.pointsToPixel(dim.getWidth());
// keep aspect ratio for height
int height = Units.pointsToPixel(dim.getHeight());
double max = Math.max(width, height);
if (max > 1500) {
width *= 1500/max;
height *= 1500/max;
}
BufferedImage bufImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bufImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
wmf.draw(g, new Rectangle2D.Double(0,0,width,height));
g.dispose();
ImageIO.write(bufImg, "PNG", new File("bla.png"));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With