Picture Frame

A picture frame is a shape that contains an image—it is like a picture in a frame.

You can add an image to a slide through a picture frame. This way, you get to format the image by formatting the picture frame.

Create Picture Frame

  1. Create an instance of the Presentation class.
  2. Get a slide’s reference through its index.
  3. Create an IPPImage object by adding an image to the IImagescollection associated with the presentation object that will be used to fill the shape.
  4. Specify the image’s width and height.
  5. Create a PictureFrame based on the image’s width and height through the AddPictureFrame method exposed by the shape object associated with the referenced slide.
  6. Add a picture frame (containing the picture) to the slide.
  7. Write the modified presentation as a PPTX file.

This C++ code shows you how to create a picture frame:

// The path to the documents directory.
const String outPath = u"../out/PictureFrameFormatting_out.pptx";
const String filePath = u"../templates/Tulips.jpg";

// LoadS the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// Accesses first slide
SharedPtr<ISlide> slide = pres->get_Slide(0);

// Loads the Image that will be added in presentaiton image collection
// Gets the picture
auto image = Images::FromFile(filePath);

// Adds an image to presentation's images collection
SharedPtr<IPPImage> imgx = pres->get_Images()->AddImage(image);

// Adds a picture frame to the slide
SharedPtr<IPictureFrame> pf = slide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 50, 50, 100, 100, imgx);

// Sets relative scale width and height
pf->set_RelativeScaleHeight(0.8);
pf->set_RelativeScaleWidth(1.35);
// Applies some formatting to PictureFrame
pf->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
pf->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Blue());
pf->get_LineFormat()->set_Width ( 20);
pf->set_Rotation( 45);

//Writes the PPTX file to disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Create Picture Frame with Relative Scale

By altering an image’s relative scaling, you can create a more complicated picture frame.

  1. Create an instance of the Presentation class.
  2. Get a slide’s reference through its index.
  3. Add an image to the presentation image collection.
  4. Create an IPPImage object by adding an image to the IImagescollection associated with the presentation object that will be used to fill the shape.
  5. Specify the image’s relative width and height in the picture frame.
  6. Write the modified presentation as a PPTX file.

This C++ code shows you how to create a picture frame with relative scale:

// The path to the documents directory.
const String outPath = u"../out/AddRelativeScaleHeightPictureFrame_out.pptx";
const String filePath = u"../templates/Tulips.jpg";

// Loads the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// Accesses the first slide
SharedPtr<ISlide> slide = pres->get_Slide(0);

// Loads the Image to be added in presentaiton image collection
// Gets the picture
auto image = Images::FromFile(filePath);

// Adds an image to the presentation's images collection
SharedPtr<IPPImage> imgx = pres->get_Images()->AddImage(image);

// Adds a picture frame to the slide
SharedPtr<IPictureFrame> pf = slide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 50, 50, 100, 100, imgx);

// Sets relative scale width and height
pf->set_RelativeScaleHeight (0.8);
pf->set_RelativeScaleWidth(1.35);

//Writes the PPTX file to disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Extract Image from Picture Frame

You can extract images from PictureFrame objects and save them in PNG, JPG, and other formats. The code example below demonstrates how to extract an image from the document “sample.pptx” and save it in PNG format.

auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto firstSlide = presentation->get_Slide(0);
auto firstShape = firstSlide->get_Shape(0);
    
if (ObjectExt::Is<IPictureFrame>(firstShape))
{
    auto pictureFrame = ExplicitCast<IPictureFrame>(firstShape);
    auto image = pictureFrame->get_PictureFormat()->get_Picture()->get_Image()->get_SystemImage();

    image->Save(u"slide_1_shape_1.png", ImageFormat::get_Png());
}

presentation->Dispose();

Get Transparency of Image

Aspose.Slides allows you to get the transparency of an image. This C++ code demonstrates the operation:

auto presentation = System::MakeObject<Presentation>(u"Test.pptx");
auto pictureFrame = System::ExplicitCast<IPictureFrame>(presentation->get_Slide(0)->get_Shape(0));
auto imageTransform = pictureFrame->get_PictureFormat()->get_Picture()->get_ImageTransform();
for (auto&& effect : imageTransform)
{
    if (System::ObjectExt::Is<IAlphaModulateFixed>(effect))
    {
        float transparencyValue = 100.0f - (System::ExplicitCast<IAlphaModulateFixed>(effect))->get_Amount();
        System::Console::WriteLine(System::String(u"Picture transparency: ") + transparencyValue);
    }
}

Picture Frame Formatting

Aspose.Slides provides many formatting options that can be applied to a picture frame. Using those options, you can alter a picture frame to make it match specific requirements.

  1. Create an instance of the Presentation class.
  2. Get a slide’s reference through its index.
  3. Create an IPPImage object by adding an image to the IImagescollection associated with the presentation object that will be used to fill the shape.
  4. Specify the image’s width and height.
  5. Create a PictureFrame based on the image’s width and height through the AddPictureFrame method exposed by the IShapes object associated with the referenced slide.
  6. Add the picture frame (containing the picture) to the slide.
  7. Set the picture frame’s line color.
  8. Set the picture frame’s line width.
  9. Rotate the picture frame by giving it either a positive or negative value.
    • A positive value rotates the image clockwise.
    • A negative value rotates the image anti-clockwise.
  10. Add the picture frame (containing the picture) to the slide.
  11. Write the modified presentation as a PPTX file.

This C++ code demonstrates the picture frame formatting process:

// The path to the documents directory.
const String outPath = u"../out/AddRelativeScaleHeightPictureFrame_out.pptx";
const String filePath = u"../templates/Tulips.jpg";

// Loads the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// Accesses the first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

// Loads the Image to be added in presentaiton image collection
// Gets the picture
auto image = Images::FromFile(filePath);

// Adds an image to the presentation's images collection
SharedPtr<IPPImage> imgx = pres->get_Images()->AddImage(image);

// Adds a picture frame to the slide
SharedPtr<IPictureFrame> pf = slide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 50, 50, 100, 100, imgx);

// Sets relative scale width and height
pf->set_RelativeScaleHeight (0.8);
pf->set_RelativeScaleWidth(1.35);

//Writes the PPTX file to disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

To avoid large presentation sizes, you can add images (or videos) through links instead of embedding the files directly into presentations. This C++ code shows you how to add an image and video into a placeholder:

auto presentation = System::MakeObject<Presentation>(u"input.pptx");
auto shapesToRemove = System::MakeObject<System::Collections::Generic::List<System::SharedPtr<IShape>>>();
auto shapes = presentation->get_Slides()->idx_get(0)->get_Shapes();

for (auto& autoShape : shapes)
{
    if (autoShape->get_Placeholder() == nullptr)
        continue;

    switch (autoShape->get_Placeholder()->get_Type())
    {
        case Aspose::Slides::PlaceholderType::Picture:
        {
            auto pictureFrame = shapes->AddPictureFrame(Aspose::Slides::ShapeType::Rectangle, autoShape->get_X(), autoShape->get_Y(), autoShape->get_Width(), autoShape->get_Height(), nullptr);
            pictureFrame->get_PictureFormat()->get_Picture()->set_LinkPathLong(u"https://upload.wikimedia.org/wikipedia/commons/3/3a/I.M_at_Old_School_Public_Broadcasting_in_October_2016_02.jpg");
            shapesToRemove->Add(autoShape);
            break;
        }

        case Aspose::Slides::PlaceholderType::Media:
        {
            auto videoFrame = shapes->AddVideoFrame(autoShape->get_X(), autoShape->get_Y(), autoShape->get_Width(), autoShape->get_Height(), u"");
            videoFrame->get_PictureFormat()->get_Picture()->set_LinkPathLong(u"https://upload.wikimedia.org/wikipedia/commons/3/3a/I.M_at_Old_School_Public_Broadcasting_in_October_2016_02.jpg");
            videoFrame->set_LinkPathLong(u"https://youtu.be/t_1LYZ102RA");
            shapesToRemove->Add(autoShape);
            break;
        }
    }
}

for (auto& shape : shapesToRemove)
{
    shapes->Remove(shape);
}

presentation->Save(u"output.pptx", Aspose::Slides::Export::SaveFormat::Pptx);

Crop Image

This C++ code shows you how to crop an existing image on a slide:

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
    
auto presentation = System::MakeObject<Presentation>();
// Creates new image object
auto newImage = presentation->get_Images()->AddImage(Images::FromFile(imagePath));

// Adds a PictureFrame to a Slide
auto picFrame = presentation->get_Slides()->idx_get(0)->get_Shapes()->AddPictureFrame(Aspose::Slides::ShapeType::Rectangle, 100.0f, 100.0f, 420.0f, 250.0f, newImage);

// Crops the image (percentage values)
picFrame->get_PictureFormat()->set_CropLeft(23.6f);
picFrame->get_PictureFormat()->set_CropRight(21.5f);
picFrame->get_PictureFormat()->set_CropTop(3.0f);
picFrame->get_PictureFormat()->set_CropBottom(31.0f);

// Saves the result
presentation->Save(outPptxFile, Aspose::Slides::Export::SaveFormat::Pptx);

Delete Cropped Areas of Picture

If you want to delete the cropped areas of an image contained in a frame, you can use the IPictureFillFormat::DeletePictureCroppedAreas() method. This method returns the cropped image or the origin image if cropping is unnecessary.

This C++ code demonstrates the operation:

System::SharedPtr<Presentation> presentation = System::MakeObject<Presentation>(u"PictureFrameCrop.pptx");
System::SharedPtr<ISlide> slide = presentation->get_Slide(0);

// Gets the PictureFrame from the first slide
System::SharedPtr<IPictureFrame> picFrame = System::AsCast<IPictureFrame>(slide->get_Shape(0));

// Deletes cropped areas of the PictureFrame image and returns the cropped image
System::SharedPtr<IPPImage> croppedImage = picFrame->get_PictureFormat()->DeletePictureCroppedAreas();

// Saves the result
presentation->Save(u"PictureFrameDeleteCroppedAreas.pptx", SaveFormat::Pptx);

Lock Aspect Ratio

If you want a shape containing an image to retain its aspect ratio even after you change the image dimensions, you can use the set_AspectRatioLocked() method to set the Lock Aspect Ratio setting.

This C++ code shows you how to lock a shape’s aspect ratio:

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>(u"pres.pptx");

System::SharedPtr<ILayoutSlide> layout = pres->get_LayoutSlides()->GetByType(SlideLayoutType::Custom);
System::SharedPtr<ISlide> emptySlide = pres->get_Slides()->AddEmptySlide(layout);

System::SharedPtr<IImage> image = Images::FromFile(u"image.png");
System::SharedPtr<IPPImage> presImage = pres->get_Images()->AddImage(image);

System::SharedPtr<IPictureFrame> pictureFrame = emptySlide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 50.0f, 150.0f, static_cast<float>(presImage->get_Width()), static_cast<float>(presImage->get_Height()), presImage);

// set shape to have to preserve aspect ratio on resizing
pictureFrame->get_PictureFrameLock()->set_AspectRatioLocked(true);

Use StretchOff Property

Using the StretchOffsetLeft, StretchOffsetTop, StretchOffsetRight and StretchOffsetBottom properties from the IPictureFillFormat interface and PictureFillFormat class, you can specify a fill rectangle.

When stretching of an image is specified, a source rectangle is scaled to fit the specified fill rectangle. Each edge of the fill rectangle is defined by a percentage offset from the corresponding edge of the shape’s bounding box. A positive percentage specifies an inset. A negative percentage specifies an outset.

  1. Create an instance of the Presentation class.
  2. Get a slide’s reference through its index.
  3. Add a rectangle AutoShape.
  4. Create an image.
  5. Set the shape’s fill type.
  6. Set the shape’s picture fill mode.
  7. Add a set image to fill the shape.
  8. Specify image offsets from the corresponding edge of the shape’s bounding box
  9. Write the modified presentation as a PPTX file.

This C++ code demonstrates a process in which a StretchOff property is used:

auto pres = System::MakeObject<Presentation>();
auto ppImage = pres->get_Images()->AddImage(Images::FromFile(u"image.png"));
auto slide = pres->get_Slide(0);
auto pictureFrame = slide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 10.0f, 10.0f, 400.0f, 400.0f, ppImage);

// Sets the image stretched from each side in the shape body
auto pictureFormat = pictureFrame->get_PictureFormat();
pictureFormat->set_PictureFillMode(PictureFillMode::Stretch);
pictureFormat->set_StretchOffsetLeft(24.0f);
pictureFormat->set_StretchOffsetRight(24.0f);
pictureFormat->set_StretchOffsetTop(24.0f);
pictureFormat->set_StretchOffsetBottom(24.0f);

pres->Save(u"imageStretch.pptx", SaveFormat::Pptx);