Free Support Forum - aspose.com

Cropping and Stretching image in a picture frame

How to use IPictureFrame to calculate the position and size of a picture in a frame

com.aspose:aspose-slides:jdk16:19.11

@chenxiaoni,

Can you please share source file so that we may further investigate to help you out.

I want to know the size and position of the image after it is changed. Now I can get the cropping percentage of the four sides of the top, bottom, left and right in IPictureFillFormat. How to calculate the exact size and position from this percentage.
picture.zip (159.6 KB)

@chenxiaoni,

I have observed your requirements and like to share that even if you perform cropping of image in PowerPoint, there will be no affect on size of image. The actual image remains intact and only visually cropping is performed. You can verify the same in PowerPoint as well. So there will be no change in size.

It seems to me that the picture inserted into PowerPoint contains a picture frame and the picture itself, and I want to know the data for setting the picture in PowerPoint.
In the data below, I can know whether the size and position of the picture are stretched, and the size and position of the picture frame.How do I get these data in Aspose.Slides for Java?
picture setting.png (5.7 KB)

@chenxiaoni,

Can you please share source presentation so that we may further investigate to help you out.

picture.zip (159.6 KB)

@chenxiaoni,

I have observed your following requirements.

Yes, any inserted image is added in images collection of presentation and then it is used for display in any normal shape or picture frame. I suggest you to please visit this documentation link for your kind reference. I have seen the requirement image in previous post. Is it possible for you to please share the required properties setting image in English translation.

PowerPoint’s Attribute
Picture Location:
Width
Height
Offset X
Offset Y
Crop Position
Width
Height
Left
Top

I can use these attributes to display in H5 pages. Use background-position and background-size to display the position, size and stretching effect of the picture in the frame

@chenxiaoni,

I have observed your requirements and suggest you to please explore PictureFormat class for cropping and offset values.

   public static void TestPicFrame()
    {


        // Instantiate presentation object
        using (Presentation presentation = new Presentation())
        {

            // Load Image to be added in presentaiton image collection
            Image img = new Bitmap( "aspose-logo.jpg");
            IPPImage image = presentation.Images.AddImage(img);

            // Add picture frame to slide
            IPictureFrame pf = presentation.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 50, 50, 100, 100, image);

            pf.FillFormat.FillType = FillType.Picture;
            pf.PictureFormat.CropLeft = 2;
            pf.PictureFormat.CropRight = 2;
            pf.PictureFormat.CropTop = 2;
            pf.PictureFormat.CropBottom = 2;
            pf.PictureFormat.StretchOffsetBottom = 1;
            pf.PictureFormat.StretchOffsetTop = 1;
            pf.PictureFormat.StretchOffsetLeft = 1;
            pf.PictureFormat.StretchOffsetRight = 1;
            pf.X = 0;
            pf.Y = 30;


            // Setting relative scale width and height
            pf.RelativeScaleHeight = 0.8f;
            pf.RelativeScaleWidth = 1.35f;

            // Save presentation
            presentation.Save( "Adding Picture Frame with Relative Scale_out.pptx", SaveFormat.Pptx);
        }

    }

@chenxiaoni,

The Java based implementation for this is as under.

  public static void TestPicFrame()
    {


        // Instantiate presentation object
        Presentation presentation = new Presentation();

        // Load Image to be added in presentaiton image collection
    	// Instantiate the Image class
    	IPPImage imgx = null;

    	try {
    		imgx = presentation.getImages().addImage(new FileInputStream(new File( "asp1.jpg")));
    	} catch (IOException e) {
    	} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      
        // Add picture frame to slide
        IPictureFrame pf = presentation.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 50, 50, 100, 100, imgx);

        pf.getFillFormat().setFillType(FillType.Picture);
        pf.getPictureFormat().setCropLeft(2);
        pf.getPictureFormat().setCropRight(2);
        pf.getPictureFormat().setCropTop(2);
        pf.getPictureFormat().setCropBottom(2);
        pf.getPictureFormat().setStretchOffsetBottom(1);
        pf.getPictureFormat().setStretchOffsetTop(1);
        pf.getPictureFormat().setStretchOffsetLeft(1);
        pf.getPictureFormat().setStretchOffsetRight(1);
        pf.setX(0);
        pf.setY(30);

        // Setting relative scale width and height
        pf.setRelativeScaleHeight(0.8f);
        pf.setRelativeScaleWidth (1.35f);

        // Save presentation
        presentation.save( "Adding Picture Frame with Relative Scale_out.pptx", SaveFormat.Pptx);

    }