Problem rendering image with size > 1 MB - Resize Image

Hi guys,
I just encountered a problem rendering images with size greater than 1MB. If using the insertImage() method of the DocumentBuilder object with no parameter to render the image, the image did not get rendered if its size > 750 KB. I then tried to resize the image as follows:

Shape shape = builder.insertImage(imageDS.getImage());
double width = shape.getWidth();
double height = shape.getHeight();
shape.setWidth(builder.getPageSetup().getPageWidth());
shape.setHeight(builder.getPageSetup().getPageWidth() * height / width);

The point is to resize the image to a width and height that are proportional to the original width and height but I got the error java.lang.IllegalArgumentException: Shape height cannot be greater than 1584 points.
Run through the debugger and it reported width = 0.75, height = 1536, builder.getPageSetup().getPageWidth() = 612.0
It looks like the getWidth() method did not return the correct value as the image I used had the size 900 KB with width = 2560 pixels and height = 2048 pixels.
Currently I have to remove the setHeight() method to get the image rendered, but the image was distorted. My question is is there a way to make the new size proportional to the original size in Aspose?
Thanks,
-Trung

Hi Trung,

Thanks for your inquiry.

I have seen this happening when inserting very large images into the document. The problem mostly stems from there being a limit on both shape size and page size (as you’ve seen from the exception).

Could you please attach your input image here for testing?

Thanks,

I have attached the image per your request. Thanks.

Hi Trung,

Thanks for your inquiry.

Yes it is the same issue. I have linked your request to the appropriate issue. In a future version will improve how large images are handled when inserted in a document.

In the mean time you can use the following code to resize such images to page boundaries. I translated it directly to Java so I apologise if some member names are incorrect, it should be fairly straight forward what members they are suppose to be.

DocumentBuilder builder = new DocumentBuilder(doc);

Shape image = builder.insertImage("image1-900KB.jpg");
resizeLargeImage(image);
public static void resizeLargeImage(Shape image)
{
    // Return if this shape is not an image.
    if (!image.hasImage())
        return;
    // Calculate the free space based on an inline or floating image. If inline we must take the page margins into account.
    PageSetup ps = image.getParentParagraph().getParentSection().getPageSetup();
    double freePageWidth = image.isInline() ? ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin() : ps.getPageWidth();
    double freePageHeight = image.isInline() ? ps.getPageHeight() - ps.getTopMargin() - ps.getBottomMargin() : ps.getPageHeight();
    // Is one of the sides of this image too big for the page?
    ImageSize size = image.getImageData().getImageSize();
    boolean exceedsMaxPageSize = size.widthPoints()> freePageWidth || size.heightPoints()> freePageHeight;
    if (exceedsMaxPageSize)
    {
        // Calculate the ratio to fit the page size based on which side is longer.
        boolean widthLonger = (size.widthPoints()> size.heightPoints());
        double ratio = widthLonger ? freePageWidth / size.widthPoints() : freePageHeight / size.heightPoints();
        // Set the new size.
        image.setWidth(size.widthPoints() * ratio);
        image.setHeight(size.heightPoints() * ratio);
    }
}

Thanks,

The issues you have found earlier (filed as WORDSNET-5962) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.