Free Support Forum - aspose.com

Replacing an Image in a PictureFrame Doubles the Image Byte Size

Hello,

When the same image is replaced within a PictureFrame, it more than doubles in byte size.

This behavior can be seen in the latest Aspose Slides for Java version 20.4, the attached prez.pptx & img1.png files and the following Java code:

  final String pptFile = [PATH] + "prez.pptx";
  final String imgFile = [PATH] + "img1.png";
  
  final IPresentation pptx = new Presentation(pptFile);
  
  // load the image bytes
  final byte[] imgBytes = Files.readAllBytes(Paths.get(imgFile));
  System.out.format("%n* * ORIGINAL image size: %,d bytes.%n", imgBytes.length);
  
  // add the image to a new slide
  ISlide slide = pptx.getSlides().get_Item(0);
  IPPImage img = pptx.getImages().addImage(imgBytes);
  IPictureFrame pf = slide.getShapes().addPictureFrame(ShapeType.Rectangle, 10, 20,
                                                       img.getWidth(), img.getHeight(),
                                                       img);
  
  System.out.format("After ADDING the image - are the image sizes equal? %B.%n",
                    imgBytes.length == img.getBinaryData().length);
  
  // Replace the newly added image using the same, original image bytes
  IPPImage existingImg = pf.getPictureFormat().getPicture().getImage();
  existingImg.replaceImage(imgBytes);
  
  // Get the bytes from the replaced image and compare to the original
  byte[] newImgBytes = existingImg.getBinaryData();
  System.out.format("After REPLACING the image - are the image sizes equal? %B.%n",
                    imgBytes.length == img.getBinaryData().length);
  System.out.format(" * * NEW image size: %,d bytes.%n%n", newImgBytes.length);
  
  // Save the new image to the file system
  final String newImgFile = imgFile.replace(".png", "_new.png");
  Files.deleteIfExists(Paths.get(newImgFile));
  Files.write(Paths.get(newImgFile), newImgBytes);
  System.out.println("Saved new image file: " + newImgFile);

Running the above code should produce the following Console output:

  * * ORIGINAL image size: 561 bytes.
  After ADDING the image - are the image sizes equal? TRUE.
  After REPLACING the image - are the image sizes equal? FALSE.
  * * NEW image size: 1,454 bytes.

Key Observations:

  • Image Byte Size: Even though the same bytes are used, the IPPImage.replaceImage API causes the image size to more than double (561 vs 1,454 bytes).
  • Images Appear Identical: In spite of the increased byte size, when visually comparing the original image to the new image, they appear identical.
  • Presentation Size: Even though the images appear identical, we’d prefer to keep the smaller image size, since our images are typically much larger than the sample image provided. The extra bytes eventually contribute towards the overall file size of the Presentation.

Environment Details:

  • Aspose Slides for Java 20.4
  • Java version 1.8.0_211
  • Windows 10 OS (but also reproducible under Linux).

File description in the ImageSize.zip (22.6 KB) attachment:

  • prez.pptx: Presentation with single slide.
  • img1.png: Original PNG image file that is added to the Presentation.
  • img1_new.png: PNG image saved in our environment after the original PNG is replaced.

Thank you!

@oraspose,

I have observed the issue shared by you and in order to further investigate this on our end a ticket with ID SLIDESJAVA-38100 has been created in our issue tracking system. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

The issues you have found earlier (filed as SLIDESJAVA-38100) have been fixed in this update.