Working with Images in Presentations — Aspose.Slides FOSS for C++

Aspose.Slides FOSS for C++ lets you embed images in a presentation’s shared image collection and display them on slides using PictureFrame shapes. Images can also be used as shape background fills via FillType::PICTURE.


Adding an Image from File

Load image bytes from disk and add them to the presentation’s image collection with pres.images().add_image(). Then place the image on a slide as a PictureFrame:

#include <Aspose/Slides/Foss/export/save_format.h>
#include <Aspose/Slides/Foss/image_collection.h>
#include <Aspose/Slides/Foss/picture_frame.h>
#include <Aspose/Slides/Foss/pp_image.h>
#include <Aspose/Slides/Foss/presentation.h>
#include <Aspose/Slides/Foss/shape_collection.h>
#include <Aspose/Slides/Foss/shape_type.h>
#include <Aspose/Slides/Foss/slide.h>
#include <Aspose/Slides/Foss/slide_collection.h>

#include <cstdint>
#include <fstream>
#include <iterator>
#include <vector>

using namespace Aspose::Slides::Foss;

int main() {
    Presentation pres;

    // Add the image to the shared collection
    std::ifstream f("logo.png", std::ios::binary);
    std::vector<std::uint8_t> data(
        (std::istreambuf_iterator<char>(f)),
        std::istreambuf_iterator<char>());
    auto& img = pres.images().add_image(data);

    // Place it on the slide as a PictureFrame
    pres.slides()[0].shapes().add_picture_frame(
        ShapeType::RECTANGLE, 50, 50, 300, 200, img);

    pres.save("with-image.pptx", SaveFormat::PPTX);
    return 0;
}

The four positional arguments to add_picture_frame() are: x, y, width, height in points.


Adding an Image from a Byte Vector

If you already have image bytes in memory (e.g., downloaded from a URL or read from a database), pass them directly to add_image():

#include <Aspose/Slides/Foss/export/save_format.h>
#include <Aspose/Slides/Foss/image_collection.h>
#include <Aspose/Slides/Foss/pp_image.h>
#include <Aspose/Slides/Foss/presentation.h>
#include <Aspose/Slides/Foss/shape_collection.h>
#include <Aspose/Slides/Foss/shape_type.h>
#include <Aspose/Slides/Foss/slide.h>
#include <Aspose/Slides/Foss/slide_collection.h>

#include <cstdint>
#include <fstream>
#include <iterator>
#include <vector>

using namespace Aspose::Slides::Foss;

int main() {
    // Simulate having bytes in memory
    std::ifstream f("photo.jpg", std::ios::binary);
    std::vector<std::uint8_t> image_bytes(
        (std::istreambuf_iterator<char>(f)),
        std::istreambuf_iterator<char>());

    Presentation pres;
    auto& img = pres.images().add_image(image_bytes);
    pres.slides()[0].shapes().add_picture_frame(
        ShapeType::RECTANGLE, 100, 80, 400, 250, img);
    pres.save("from-bytes.pptx", SaveFormat::PPTX);
    return 0;
}

Positioning and Sizing a PictureFrame

The PictureFrame returned by add_picture_frame() inherits all Shape geometry properties and can be repositioned after creation:

#include <Aspose/Slides/Foss/export/save_format.h>
#include <Aspose/Slides/Foss/image_collection.h>
#include <Aspose/Slides/Foss/picture_frame.h>
#include <Aspose/Slides/Foss/pp_image.h>
#include <Aspose/Slides/Foss/presentation.h>
#include <Aspose/Slides/Foss/shape_collection.h>
#include <Aspose/Slides/Foss/shape_type.h>
#include <Aspose/Slides/Foss/slide.h>
#include <Aspose/Slides/Foss/slide_collection.h>

#include <cstdint>
#include <fstream>
#include <iterator>
#include <vector>

using namespace Aspose::Slides::Foss;

int main() {
    Presentation pres;
    std::ifstream f("photo.jpg", std::ios::binary);
    std::vector<std::uint8_t> data(
        (std::istreambuf_iterator<char>(f)),
        std::istreambuf_iterator<char>());
    auto& img = pres.images().add_image(data);

    auto& pf = pres.slides()[0].shapes().add_picture_frame(
        ShapeType::RECTANGLE, 0, 0, 100, 100, img);

    // Reposition and resize after creation
    pf.set_x(50);
    pf.set_y(100);
    pf.set_width(350);
    pf.set_height(250);

    pres.save("positioned.pptx", SaveFormat::PPTX);
    return 0;
}

Using an Image as a Shape Fill

Any shape (not just PictureFrame) can use an image as its background fill. Set fill_type to FillType::PICTURE and assign the image to the picture fill format:

#include <Aspose/Slides/Foss/auto_shape.h>
#include <Aspose/Slides/Foss/export/save_format.h>
#include <Aspose/Slides/Foss/fill_format.h>
#include <Aspose/Slides/Foss/fill_type.h>
#include <Aspose/Slides/Foss/image_collection.h>
#include <Aspose/Slides/Foss/picture_fill_mode.h>
#include <Aspose/Slides/Foss/pp_image.h>
#include <Aspose/Slides/Foss/presentation.h>
#include <Aspose/Slides/Foss/shape_collection.h>
#include <Aspose/Slides/Foss/shape_type.h>
#include <Aspose/Slides/Foss/slide.h>
#include <Aspose/Slides/Foss/slide_collection.h>

#include <cstdint>
#include <fstream>
#include <iterator>
#include <vector>

using namespace Aspose::Slides::Foss;

int main() {
    Presentation pres;
    std::ifstream f("background.png", std::ios::binary);
    std::vector<std::uint8_t> data(
        (std::istreambuf_iterator<char>(f)),
        std::istreambuf_iterator<char>());
    auto& img = pres.images().add_image(data);

    auto& slide = pres.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        ShapeType::ROUND_CORNER_RECTANGLE, 50, 50, 400, 250);
    shape.fill_format().set_fill_type(FillType::PICTURE);
    shape.fill_format().picture_fill_format().set_picture_fill_mode(PictureFillMode::STRETCH);
    shape.fill_format().picture_fill_format().picture().set_image(&img);

    pres.save("picture-fill.pptx", SaveFormat::PPTX);
    return 0;
}

PictureFillMode::STRETCH scales the image to fill the entire shape. Use TILE for repeating patterns.


See Also