C++에서 PowerPoint 슬라이드에 이미지 추가하는 방법
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&모든 치수는 포인트(1 포인트 = 1/72 인치) 단위입니다. 표준 10 x 7.5인치 슬라이드의 좌표 공간은 720 x 540 포인트입니다.
메모리 버퍼에서 이미지 추가
이미지를 바이트 형태로 이미 가지고 있는 경우(예: 네트워크에서 다운로드했거나 데이터베이스에서 읽은 경우):
#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;
}채우기 모드 제어
PictureFrame에 있는 picture_format()은 이미지가 프레임 경계에 채워지는 방식을 제어합니다:
#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() 컬렉션은 모든 슬라이드에서 공유됩니다: 그림 프레임이 여러 슬라이드에 나타나더라도 동일한 이미지 바이트가 한 번만 저장됩니다.