@@ -, +, @@ - useful information when resizing anchors. - simply moved some code from XSLFPictureShape to XSLFPictureData and cache the values there. --- .../apache/poi/xslf/usermodel/XSLFPictureData.java | 34 ++++++++++++++++++++ .../poi/xslf/usermodel/XSLFPictureShape.java | 25 +++++++------- 2 files changed, 47 insertions(+), 12 deletions(-) --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java +++ a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java @@ -19,9 +19,12 @@ package org.apache.poi.xslf.usermodel; +import java.awt.Dimension; +import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import javax.imageio.ImageIO; import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLException; @@ -37,6 +40,9 @@ import org.apache.poi.util.IOUtils; @Beta public final class XSLFPictureData extends POIXMLDocumentPart implements PictureData { private Long checksum = null; + + // original image dimensions (for formats supported by BufferedImage) + private Dimension _origSize = null; private int index = -1; /** @@ -115,6 +121,32 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture } /** + * Return the original image dimensions + * (for formats supported by BufferedImage). + * + * Will return a Dimension with zero width/height if the format unsupported. + */ + public Dimension getPictureDimension() + { + if (_origSize == null) + { + try + { + BufferedImage img = ImageIO.read(getInputStream()); + _origSize = new Dimension(img.getWidth(), img.getHeight()); + } + catch (Exception failed) + { + // failed to get information, set as zero size + _origSize = new Dimension(); + } + } + + return _origSize; + } + + + /** * *PictureData objects store the actual content in the part directly without keeping a * copy like all others therefore we need to handle them differently. */ @@ -134,6 +166,8 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture os.close(); // recalculate now since we already have the data bytes available anyhow checksum = IOUtils.calculateChecksum(data); + + _origSize = null; // need to recalculate image size } @Override --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java +++ a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java @@ -19,12 +19,10 @@ package org.apache.poi.xslf.usermodel; +import java.awt.Dimension; import java.awt.Insets; -import java.awt.geom.Rectangle2D; -import java.awt.image.BufferedImage; +import java.awt.Rectangle; import java.net.URI; - -import javax.imageio.ImageIO; import javax.xml.namespace.QName; import org.apache.poi.POIXMLException; @@ -86,20 +84,23 @@ public class XSLFPictureShape extends XSLFSimpleShape implements PictureShape { /** * Resize this picture to the default size. + * * For PNG and JPEG resizes the image to 100%, - * for other types sets the default size of 200x200 pixels. + * for other types sets the default size to 200x200 pixels. */ public void resize() { - try { - BufferedImage img = ImageIO.read(getPictureData().getInputStream()); - setAnchor(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight())); + Dimension dim = getPictureData().getPictureDimension(); + if (dim.width > 0 && dim.height > 0) + { + setAnchor(new Rectangle(0, 0, dim.width, dim.height)); } - catch (Exception e) { - //default size is 200x200 - setAnchor(new java.awt.Rectangle(50, 50, 200, 200)); + else + { + // unsupported/unknown formats + setAnchor(new Rectangle(50, 50, 200, 200)); } } - + /** * Is this an internal picture (image data included within * the PowerPoint file), or an external linked picture --