Werken met afbeeldingen in presentaties — Aspose.Slides FOSS voor C++
Aspose.Slides FOSS for C++ lets you embed images in a presentation’s shared image collection and display them on slides using PictureFrame vormen. Afbeeldingen kunnen ook worden gebruikt als vorm-achtergrondvullingen via FillType::PICTURE.
Een afbeelding toevoegen vanuit bestand
Laad afbeeldingsbytes van schijf en voeg ze toe aan de afbeeldingencollectie van de presentatie met pres.images().add_image(). Plaats vervolgens de afbeelding op een dia als een 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;
}De vier positionele argumenten voor add_picture_frame() zijn: x, y, width, height in punten.
Een afbeelding toevoegen vanuit een bytevector
Als je al afbeeldingsbytes in het geheugen hebt (bijv. gedownload van een URL of gelezen uit een database), geef ze direct door aan 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;
}Positioneren en dimensioneren van een PictureFrame
De PictureFrame geretourneerd door add_picture_frame() erft alle Shape geometrie-eigenschappen en kan na creatie opnieuw worden gepositioneerd:
#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;
}Een afbeelding gebruiken als vormvulling
Elke vorm (niet alleen PictureFrame) kan een afbeelding gebruiken als achtergrondvulling. Stel fill_type naar FillType::PICTURE en wijs de afbeelding toe aan het picture fill-formaat:
#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 schalen de afbeelding zodat deze de hele vorm vult. Gebruik TILE voor herhalende patronen.