Πώς να Προσθέσετε Εικόνες σε Διαφάνειες PowerPoint σε C++
Οι εικόνες στο Aspose.Slides FOSS ενσωματώνονται ως picture frames, σχήματα που κρατούν μια εικόνα και μπορούν να τοποθετηθούν, να αλλάξουν μέγεθος και να μορφοποιηθούν όπως οποιοδήποτε άλλο σχήμα. Τα δεδομένα της εικόνας αποθηκεύονται μία φορά στη συλλογή prs.images() και αναφέρονται από το πλαίσιο.
Προαπαιτούμενα
git clone https://github.com/aspose-slides-foss/Aspose.Slides-FOSS-for-Cpp.git
cd Aspose.Slides-FOSS-for-Cpp && mkdir build && cd build
cmake .. && cmake --build .Προσθήκη εικόνας από αρχείο
#include <Aspose/Slides/Foss/presentation.h>
#include <fstream>
#include <vector>
int main() {
namespace asf = Aspose::Slides::Foss;
asf::Presentation prs;
auto& slide = prs.slides()[0];
// Load image into the presentation's image collection
std::ifstream file("photo.jpg", std::ios::binary);
std::vector<uint8_t> data((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
auto& img = prs.images().add_image(data);
// Add a picture frame at (x=50, y=50, width=400, height=300) in points
slide.shapes().add_picture_frame(
asf::ShapeType::RECTANGLE, 50, 50, 400, 300, img);
prs.save("with-image.pptx", asf::SaveFormat::PPTX);
return 0;
}Η add_picture_frame υπογραφή:
add_picture_frame(shape_type, x, y, width, height, image) -> PictureFrame&Όλες οι διαστάσεις είναι σε points (1 point = 1/72 ίντσα). Για μια τυπική διαφάνεια 10 x 7,5 ίντσες, ο χώρος συντεταγμένων είναι 720 x 540 points.
Προσθήκη εικόνας από buffer μνήμης
Εάν έχετε ήδη την εικόνα ως bytes (π.χ., ληφθείσα από δίκτυο ή αναγνωσμένη από βάση δεδομένων):
#include <Aspose/Slides/Foss/presentation.h>
#include <vector>
int main() {
namespace asf = Aspose::Slides::Foss;
// Assume image_bytes is populated from some source
std::vector<uint8_t> image_bytes = /* ... */;
asf::Presentation prs;
auto& img = prs.images().add_image(image_bytes);
prs.slides()[0].shapes().add_picture_frame(
asf::ShapeType::RECTANGLE, 200, 100, 300, 200, img);
prs.save("logo-slide.pptx", asf::SaveFormat::PPTX);
return 0;
}Έλεγχος της λειτουργίας γεμίσματος
Το picture_format() σε ένα PictureFrame ελέγχει πώς η εικόνα γεμίζει τα όρια του πλαισίου:
#include <Aspose/Slides/Foss/presentation.h>
#include <fstream>
#include <vector>
int main() {
namespace asf = Aspose::Slides::Foss;
asf::Presentation prs;
std::ifstream file("texture.png", std::ios::binary);
std::vector<uint8_t> data((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
auto& img = prs.images().add_image(data);
auto& frame = prs.slides()[0].shapes().add_picture_frame(
asf::ShapeType::RECTANGLE, 50, 50, 600, 350, img);
// STRETCH: scale image to fill the frame exactly (default)
frame.picture_format().set_picture_fill_mode(asf::PictureFillMode::STRETCH);
// TILE: repeat the image in a grid pattern
// frame.picture_format().set_picture_fill_mode(asf::PictureFillMode::TILE);
prs.save("filled.pptx", asf::SaveFormat::PPTX);
return 0;
}PictureFillMode | Συμπεριφορά |
|---|---|
STRETCH | Κλιμακώστε την εικόνα ώστε να γεμίσει το πλαίσιο, αγνοώντας την αναλογία διαστάσεων |
TILE | Επαναλάβετε την εικόνα ως μοτίβο πλακιδίων |
Προσθήκη Πολλαπλών Εικόνων σε Διαφορετικές Διαφάνειες
#include <Aspose/Slides/Foss/presentation.h>
#include <fstream>
#include <vector>
#include <string>
#include <filesystem>
int main() {
namespace asf = Aspose::Slides::Foss;
std::vector<std::string> image_files = {"slide1.jpg", "slide2.jpg", "slide3.jpg"};
asf::Presentation prs;
auto& layout = prs.slides()[0].layout_slide();
// Ensure enough slides exist
while (prs.slides().size() < image_files.size()) {
prs.slides().add_empty_slide(layout);
}
for (size_t i = 0; i < image_files.size(); ++i) {
if (!std::filesystem::exists(image_files[i])) continue;
std::ifstream file(image_files[i], std::ios::binary);
std::vector<uint8_t> data((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
auto& img = prs.images().add_image(data);
prs.slides()[i].shapes().add_picture_frame(
asf::ShapeType::RECTANGLE, 0, 0, 720, 540, img);
}
prs.save("multi-image.pptx", asf::SaveFormat::PPTX);
return 0;
}Καταμέτρηση εικόνων σε υπάρχουσα παρουσίαση
#include <Aspose/Slides/Foss/presentation.h>
#include <iostream>
int main() {
namespace asf = Aspose::Slides::Foss;
asf::Presentation prs("with-image.pptx");
std::cout << "Presentation contains " << prs.images().size() << " image(s)\n";
return 0;
}Η συλλογή prs.images() μοιράζεται σε όλες τις διαφάνειες: τα ίδια bytes εικόνας αποθηκεύονται μία φορά ακόμη και αν το πλαίσιο εικόνας εμφανίζεται σε πολλαπλές διαφάνειες.