Arbeiten mit Bildern in Präsentationen Aspose.Slides FOSS für C++

Aspose.Slides FOSS for C++ lets you embed images in a presentation’s shared image collection and display them on slides using PictureFrame Bilder können auch als Form Hintergrund füllt über die FillType::PICTURE.


Ein Bild aus einer Datei hinzufügen

Laden Sie Bildbyte von der Festplatte und fügen sie zur Präsentationsbild-Sammlung mit pres.images().add_image().Dann platzieren Sie das Bild auf einer Folie als eine 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;
}

Die vier Positionsargumente für die add_picture_frame() sind: x, y, width, height in Punkten.


Hinzufügen eines Bildes aus einem Byte-Vektor

Wenn Sie bereits Bildbyte im Speicher haben (z. B. von einer URL heruntergeladen oder aus einer Datenbank gelesen), geben Sie sie direkt an die 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;
}

Positionierung und Größe eines Bildrahmens

Die PictureFrame zurückgegeben von: add_picture_frame() Erbt alle Shape Geometrie-Eigenschaften und kann nach der Erstellung neu positioniert werden:

#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;
}

Ein Bild als Formfüllung zu verwenden

Jede Form (nicht nur PictureFrame) kann ein Bild als Hintergrundfüllung verwenden. Setzen Sie die Schnittstelle für das Bild, um den Hundertspiel zu erstellen. fill_type zu . FillType::PICTURE und das Bild dem Format des Bildfüllformats zuordnen:

#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 Das Bild wird so skaliert, dass es die gesamte Form füllt. TILE für wiederholte Muster.


Siehe auch:

 Deutsch