diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java b/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java index f1b9b3a4df..3be79efbd1 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java @@ -122,12 +122,19 @@ public class Picture extends SimpleShape { public void setDefaultSize(){ PictureData pict = getPictureData(); if (pict instanceof Bitmap){ + BufferedImage img = null; try { - BufferedImage img = ImageIO.read(new ByteArrayInputStream(pict.getData())); - if(img != null) setAnchor(new java.awt.Rectangle(0, 0, img.getWidth(), img.getHeight())); - else setAnchor(new java.awt.Rectangle(0, 0, 200, 200)); - } catch (IOException e){ - ; + img = ImageIO.read(new ByteArrayInputStream(pict.getData())); + } + catch (IOException e){} + catch (NegativeArraySizeException ne) {} + + if(img != null) { + // Valid image, set anchor from it + setAnchor(new java.awt.Rectangle(0, 0, img.getWidth(), img.getHeight())); + } else { + // Invalid image, go with the default metafile size + setAnchor(new java.awt.Rectangle(0, 0, 200, 200)); } } else { //default size of a metafile picture is 200x200 diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java index 6767a299b0..91f4f1f509 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java @@ -257,6 +257,11 @@ public class TestPictures extends TestCase{ Slide slide = ppt.createSlide(); File img = new File(cwd, "sci_cec.dib"); + + // Check we can read the test DIB image + assertTrue(img.exists()); + + // Add the image int idx = ppt.addPicture(img, Picture.DIB); Picture pict = new Picture(idx); assertEquals(idx, pict.getPictureIndex());