Re-Sizing Images



I have to include Images in the report on the fly and these images can be of different sizes. Currently the code I am using just changes the size of the image to the image size which is being used in the template. This results in the actual image being enlarged if its too small or the image shrunks if too big. I need to put some code which checks if the image is smaller than the image size in the template then just paste the image without changing the size but if the image is troo big then change the size to the size of the image in the template. Can you Please help?


You can try something like this:

Image img = …; // image
float sizeX = …; // image size in the template
float sizeY = …;
if ((float)img.Width * 576f / img.HorizontalResolution <= sizeX
&& (float)img.Height * 576f / img.VerticalResolution <= sizeY)
// don’t change image size


I am trying to implement what you have suggested but I am stuck with 1 issue. I had received the aspose.powerpoint.jar file from a friend a while back. But this version does not seem to be the latest as it does not match the API JavaDocs available on the ASPOSE site. Also, in the downloads section I cant find “ASPSOE PowerPoint” and I cant find a place where I can download the latest jar file. Please help!


Aspose.PowerPoint was renamed to Aspose.Slides.
Don’t forget to change imports because name of package was renamed too.


I downloaded the aspose.slides.jar file. But haveing issues with that. I ahve the following code:

{ file = new;
System.out.println("===Test 11==="+file);
FileInputStream istream = new FileInputStream(file);
System.out.println("===Test 22==="+istream);
pres = new Presentation(istream);
System.out.println("===Test 33===");

catch(Exception e)
System.out.println("===Report props Exception=" + e);
System.out.println("===In finally=");

Following lines gets printed:

===Test 11===C:\Tomcat 4.1\webapps\ematrix\programcentral\custom\Hayes_SalesPres
===In finally=

Please Note the constructor for Presentation does not throw anyException but is not successful too. This same code used to work for the "aspose.powerpoint.jar" file that I had previously. Please help !


It doesn’t throw Exception and not successfull in the same time. How you think it’s possible?
Try to recompile this class or run it from debugger and check where is the problem.


My wrong ! I was running this is a web app and not sure how the exception was not being trapped. Anyways, I tried running this via a simple java app and here is teh Exception messgae:

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/aspose/sl
ides/Presentation (Unsupported major.minor version 48.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at Source)
at Source)
at$100(Unknown Source)
at$ Source)
at Method)
at Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at PresTest.Load(
at PresTest.main(

When I used the command "java -version", the output I got was:

java version "1.3.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_01)
Java HotSpot(TM) Client VM (build 1.3.1_01, mixed mode)

What version of java do I need?


You should use Sun’s jvm 1.4 or 1.5.


I have been able to make a lot of progress but I need help with the following:

If the Width & height of the image is smaller than the image in the template, I do not change the image size. But the issue with this is the placement of the image. The new image gets placeed in the upper left corner of the pictureframe instead of the center of the pictureframe. Having the width, height of the tempalte image and the new image and the X and Y coordinates of the template image, how can I make sur ethat the picture get splaced in the center of the picture frame? Please help.


For the formula you have suggested:

if ((float)img.Width * 576f / img.HorizontalResolution <= sizeX
&& (float)img.Height * 576f / img.VerticalResolution <= sizeY)
// don't change image size

I have assumed the HorizontalResolution and VerticalResolution to be 96. Is that right? If not, how do I find HorizontalResolution and VerticalResolution of an image?


HorizontalResolution and VerticalResolution are public properties in the Image class

For moving image to the center you can simply change position and size of PictureFrame.


The link that you gave me for finding out the HorizontalResolution and VerticalResolution is for .NET but I am using Java.

Ragrding moving the image position, do you have any example code for calcuating the center point of the PictureFrame and then placing the new image in the center of that pictureframe?



Sorry, for Java you can try this one for example:

Probably you didn’t understand. Pictures are always scaled to the
whole size of PictureFrame so you don’t need to calculate center.
Just change position and size of frame to values you need for your image.