Convert EMF to PNG, result is not perfect

539 views Asked by At

I am trying to convert EMF file to PNG, with half success. With the code I am able to create a PNG picture from EMF, but the result is not perfect: compared to the original it is "squeezed" horizontally + on the right side of the picture some columns of pixels are missing. (seems like it cut out somehow, don't know why)

Any idea?

My code:

        File f = new File(PathToImage);
        try (FileInputStream fis = new FileInputStream(f)) {
            HemfPicture emf = new HemfPicture(fis);
            Dimension2D dim = emf.getSize();
            int width = Units.pointsToPixel(dim.getWidth());
            int height = Units.pointsToPixel(dim.getHeight());

            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);
            emf.draw(g, new Rectangle2D.Double(0, 0, width, height));
            g.dispose();

            ImageIO.write(bufImg, "PNG", new File(
                    path + imageName + ".png"));
        }

1

There are 1 answers

0
Shephard On BEST ANSWER

The problem was that I used an older version of Apache POI. I tried Apache POI 5.0.0 and the result is much better.

Maven:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.0.0</version>
</dependency>