Image resize in ppt

Hi

could you please help me with Resize images with some particular aspect ratio without losing the image
resolution and quality in ppt slide.

Regards,
Dharmender

Hi Dharmender,

I have observed the requirements shared by you and like to share that the images are added in presentation image collection with actual size. Can you please share the details about your requirement by sharing a sample presentation. I will try my best to help you further in this regard.

Many Thanks,

Hi,


Thanks for your quick response.

Our requirement is like, I have presentation with one slide. The slide will have some slots in it like Chart, Table, Image like in the attached presentation file.

So I have to replace the image slot with the actual image (the image will be on shared drive). Before replacing the image i need to store the height, width, X & Y coordinates of the Image slot in the slide. So that i can insert with image with the same size of the slot without loosing the quality of my image.

Can you please help me in this regard.

Hi Dharmender,

I have further observed the requirements sheared by you from the sample presentation. I have generated the sample code that actually finds the desired shape using Alternative Text property and then fill the shape with actual source image dimensions. Please use the following sample code and share with us if I may help you further in this regard.

public static void replaceImage()
{
String path=“D:\Aspose Data\”;
PresentationEx pres=new PresentationEx(path+“demo_dharma.pptx”);

SlideEx slide=pres.getSlides().get_Item(0);

ShapeEx shape = FindShape(slide, “image”);

//Creating a Buffered Image object to hold the image file
BufferedImage image=null;

try {
image = ImageIO.read(new File(“C:\Users\Public\Pictures\Sample Pictures\Desert.jpg”));
} catch (IOException e) {
}

ImageEx imgx = pres.getImages().addImage(image);


if(shape instanceof PictureFrameEx)
{
//now setting the new image to identified shape and setting coordinates
PictureFrameEx picFrame=(PictureFrameEx)shape;

picFrame.setHeight(imgx.getHeight());
picFrame.setWidth(imgx.getWidth());

picFrame.getFillFormat().getPictureFillFormat().getPicture().setImage(imgx);

}

else if(shape instanceof AutoShapeEx)
{
AutoShapeEx ashp=(AutoShapeEx)shape;
ashp.setHeight(imgx.getHeight());
ashp.setWidth(imgx.getWidth());
ashp.getFillFormat().setFillType(FillTypeEx.Picture);
ashp.getFillFormat().getPictureFillFormat().getPicture().setImage(imgx);

}
//Write the PPTX file to disk
pres.write(path+“GeneratedPres.pptx”);

}

//Method implementation to find a shape in a slide using its alternative text
static ShapeEx FindShape(SlideEx slide, String alttext)
{
//Iterating through all shapes inside the slide
for (int i = 0; i < slide.getShapes().getCount(); i++)
{


//If the alternative text of the slide matches with the required one then
//return the shape
if (slide.getShapes().get_Item(i).getAlternativeText().equals(alttext))
return slide.getShapes().get_Item(i);
}
return null;
}

Many Thanks,

Hi,

I tried with the sample code which you have sent. But i am not able to set the image in the actual shape of the slide. The image is getting overlapped. I am attaching the input ppt and the output ppt files for your reference.

public static void replaceImage()
{
String dataDir = “src/updatinganexistingchart/data/”;
PresentationEx pres=new PresentationEx(dataDir+“demo.pptx”);

SlideEx slide=pres.getSlides().get_Item(0);

ShapeEx shape = FindShape(slide, “Picture_WD”);

//Creating a Buffered Image object to hold the image file
BufferedImage image=null;
try {

image = ImageIO.read(new File(dataDir+“Chrysanthemum.jpg”));
} catch (IOException e) {
} catch (AsposeLicenseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

ImageEx imgx = pres.getImages().addImage(image);


if(shape instanceof PictureFrameEx)
{
//now setting the new image to identified shape and setting coordinates
PictureFrameEx picFrame=(PictureFrameEx)shape;

picFrame.setHeight(imgx.getHeight());
picFrame.setWidth(imgx.getWidth());
picFrame.getFillFormat().setFillType(FillTypeEx.Picture);
picFrame.getFillFormat().getPictureFillFormat().getPicture().setImage(imgx);

}

else if(shape instanceof AutoShapeEx)
{
AutoShapeEx ashp=(AutoShapeEx)shape;
ashp.setHeight(imgx.getHeight());
ashp.setWidth(imgx.getWidth());
ashp.getFillFormat().setFillType(FillTypeEx.Picture);
ashp.getFillFormat().getPictureFillFormat().getPicture().setImage(imgx);
}
//Write the PPTX file to disk
pres.write(dataDir+“GeneratedPres.pptx”);
}

//Method implementation to find a shape in a slide using its alternative text
static ShapeEx FindShape(SlideEx slide, String alttext)
{
//Iterating through all shapes inside the slide
for (int i = 0; i < slide.getShapes().getCount(); i++)
{ //If the alternative text of the slide matches with the required one then
//return the shape
if (slide.getShapes().get_Item(i).getName().equals(alttext))
return slide.getShapes().get_Item(i);
}
return null;
}

I did some changes to the code, I am passing the shape name (Picture_WD) as a parameter to the method FindShape method and getting the required shape. After getting the required shape, i have replace this shape with the actual image. But the image should not overlap, it is should fit with the same height and width of the initial shape.

I hope you understand my requirement.


Please let me know if you require any further information.

Hi,

This is the output what i am getting. I want the new image which i am passing from shared location to be replaced with the existing one. But the size and width should be as existing one.

Hi,

I have further investigated the requirements shared by you with reference to our conversation over live chat session. Actually, you need to set either unique slide name or unique alternative text property to identify and find the shape. Please use the attached sample presentations and modified sample code on your end to serve the purpose. Please share, if I may help you further in this regard.

public static void replaceImage()
{
String dataDir = “D:/Aspose Data/poc/”;
PresentationEx pres=new PresentationEx(dataDir+“demo.pptx”);

SlideEx slide=pres.getSlides().get_Item(0);

// ShapeEx shape = FindShape(slide, “Picture_WD1”);
ShapeEx shape = FindShape(slide, “Picture_WD1”);

//Creating a Buffered Image object to hold the image file
BufferedImage image=null;
try {

image = ImageIO.read(new File(dataDir+“Chrysanthemum.jpg”));
} catch (IOException e) {
e.printStackTrace();;
}
ImageEx imgx = pres.getImages().addImage(image);


if(shape instanceof PictureFrameEx)
{
//now setting the new image to identified shape and setting coordinates
PictureFrameEx picFrame=(PictureFrameEx)shape;

// picFrame.setHeight(imgx.getHeight());
// picFrame.setWidth(imgx.getWidth());
picFrame.getFillFormat().setFillType(FillTypeEx.Picture);
// picFrame.getFillFormat().getPictureFillFormat().getPicture().setImage(imgx);
picFrame.getPictureFormat().getPicture().setImage(imgx);
// slide.getShapes().addPictureFrame(ShapeTypeEx.Rectangle,0f, 0f, 100f, 100f, imgx);

}

else if(shape instanceof AutoShapeEx)
{
AutoShapeEx ashp=(AutoShapeEx)shape;
// ashp.setHeight(imgx.getHeight());
// ashp.setWidth(imgx.getWidth());
ashp.getFillFormat().setFillType(FillTypeEx.Picture);
ashp.getFillFormat().getPictureFillFormat().getPicture().setImage(imgx);

}
//Write the PPTX file to disk
pres.write(dataDir+“GeneratedPres2.pptx”);
}


Many Thanks,

Hi,

Thanx for your reply. I tried the above code and it worked, however the quality of the image is lost. The image is getting stretched. I don’t want this to happen.

And would like to know the use of the below statement.

else if(shape instanceof AutoShapeEx)
{
AutoShapeEx ashp=(AutoShapeEx)shape;
// ashp.setHeight(imgx.getHeight());
// ashp.setWidth(imgx.getWidth());
ashp.getFillFormat().setFillType(FillTypeEx.Picture);
ashp.getFillFormat().getPictureFillFormat().getPicture().setImage(imgx);

}

Hi,

I have observed the observation shared by you. This is in fact not an issue. When you are trying to fit an image inside a shape the image will inherit the aspect ration of shape. That is why you are getting stretched image on your end. One possible solution for this can be that you adjust the height of shape by keeping the width of shape constant w.r.t aspect ratio of source image. You may please try using following on your end to serve the purpose.

AutoShapeEx ashp=(AutoShapeEx)shape;
double aspectRatio=(double)(imgx.getWidth()/imgx.getHeight());
ashp.setHeight( ashp.getWidth()/aspectRatio);
// ashp.setWidth(imgx.getWidth());
ashp.getFillFormat().setFillType(FillTypeEx.Picture);
ashp.getFillFormat().getPictureFillFormat().getPicture().setImage(imgx);

Many Thanks,

Hi

I am getting the below error using the above code.
Exception in thread “main” java.lang.ClassCastException: com.aspose.slides.PictureFrameEx cannot be cast to com.aspose.slides.AutoShapeEx
at updatinganexistingchart.java.ImageResize.replaceImage(ImageResize.java:48)
at updatinganexistingchart.java.ImageResize.main(ImageResize.java:23)


so I change something like this

if(shape instanceof PictureFrameEx)
{
//now setting the new image to identified shape and setting coordinates
PictureFrameEx picFrame=(PictureFrameEx)shape;
// picFrame.getFillFormat().setFillType(FillTypeEx.Picture);
// picFrame.getPictureFormat().getPicture().setImage(imgx);
double aspectRatio=(double)(imgx.getWidth()/imgx.getHeight());
picFrame.setHeight( (float) (picFrame.getWidth()/aspectRatio));
picFrame.getFillFormat().setFillType(FillTypeEx.Picture);
picFrame.getPictureFormat().getPicture().setImage(imgx);
}

however it is overlapping with other shapes in the slide.

I would need this as output.

1. The image should not loose the resolution.
2. If the input image is smaller/larger it should be adjusted in the frame, however should not loose the resolution.
example:
1. If the input image is of resolution 3X4 and the frame is 16:9. The output should be centered by cutting the right and left sides.
2. If the input image is smaller in size than the size of the frame. The output should be center of the frame by cutting all the sides.

Hope you understood the requirement. Please do let me know for any questions.

Hi Dharmender,


I have observed the issue shared by you and have worked over your requirements. Please try using the following sample code on your end serve the purpose.

public static void replaceImage()
{
String dataDir = “D:/Aspose Data/”;
PresentationEx pres=new PresentationEx(dataDir+“demo.pptx”);

SlideEx slide=pres.getSlides().get_Item(0);

ShapeEx shape = FindShape(slide, “Picture_WD1”);

//Creating a Buffered Image object to hold the image file
BufferedImage image=null;
try {
image = ImageIO.read(new File(dataDir+“Chrysanthemum.jpg”));
} catch (IOException e) {
e.printStackTrace();;
}
ImageEx imgx = pres.getImages().addImage(image);


if(shape instanceof PictureFrameEx)
{
//now setting the new image to identified shape and setting coordinates
PictureFrameEx picFrame=(PictureFrameEx)shape;
float aspectRatio=((float)imgx.getWidth()/(float)imgx.getHeight());
picFrame.setHeight( (float)(picFrame.getWidth()/aspectRatio));
picFrame.getFillFormat().setFillType(FillTypeEx.Picture);
picFrame.getPictureFormat().getPicture().setImage(imgx);
}

else if(shape instanceof AutoShapeEx)
{
AutoShapeEx ashp=(AutoShapeEx)shape;
float aspectRatio=(float)imgx.getWidth()/(float)imgx.getHeight();
ashp.setHeight( (float)(ashp.getWidth()/aspectRatio));
// ashp.setWidth(imgx.getWidth());
ashp.getFillFormat().setFillType(FillTypeEx.Picture);
ashp.getFillFormat().getPictureFillFormat().getPicture().setImage(imgx);
}
//Write the PPTX file to disk
pres.write(dataDir+“GeneratedPres2.pptx”);
}

Please note that in the shared sample code, I have adjusted the shape height by keeping shape width constant in accordance with the aspect ration of source image. It will resolve the image stretching issue but will change the height of shape which may get overlapped on other shapes. This is neither an issue with PowerPoint nor with Aspose.Slides but a requirement to be met as per request.

Please share, if I may help you further in this regard.

Many Thanks,