Working with Images in Presentations — Aspose.Slides FOSS for C++ => <<<SEG_1>>> Lập việc với Hình ảnh trong Bài thuyết trình — Aspose.Slides FOSS cho C++

Aspose.Slides FOSS for C++ lets you embed images in a presentation’s shared image collection and display them on slides using PictureFrame hình dạng. Hình ảnh cũng có thể được sử dụng làm nền cho hình dạng thông qua FillType::PICTURE.


Thêm hình ảnh từ tệp

Tải byte hình ảnh từ đĩa và thêm chúng vào bộ sưu tập hình ảnh của bản trình chiếu bằng pres.images().add_image(). Sau đó đặt hình ảnh lên một slide dưới dạng 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;
}

Bốn đối số vị trí cho add_picture_frame() là: x, y, width, height đơn vị điểm.


Thêm hình ảnh từ một vector byte

Nếu bạn đã có byte hình ảnh trong bộ nhớ (ví dụ: tải xuống từ URL hoặc đọc từ cơ sở dữ liệu), hãy truyền chúng trực tiếp tới 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;
}

Định vị và thay đổi kích thước một PictureFrame

Đối tượng PictureFrame được trả về bởi add_picture_frame() kế thừa tất cả Shape các thuộc tính hình học và có thể được đặt lại vị trí sau khi tạo:

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

Sử dụng hình ảnh làm nền cho hình dạng

Bất kỳ hình dạng nào (không chỉ PictureFrame) cũng có thể sử dụng hình ảnh làm nền. Đặt fill_type thành FillType::PICTURE và gán hình ảnh cho định dạng nền ảnh:

#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 phóng to hình ảnh để lấp đầy toàn bộ hình dạng. Sử dụng TILE để tạo các mẫu lặp lại.


Xem Thêm

 Tiếng Việt