การทำงานกับรูปภาพในงานนำเสนอ — Aspose.Slides FOSS สำหรับ Java

Aspose.Slides FOSS for Java lets you embed images in a presentation’s shared image collection and display them on slides using PictureFrame รูปร่าง. ภาพยังสามารถใช้เป็นการเติมพื้นหลังของรูปร่างผ่าน FillType.PICTURE.


การเพิ่มรูปภาพจากไฟล์

โหลดไบต์ของภาพจากดิสก์และเพิ่มลงในคอลเลกชันภาพของงานนำเสนอด้วย prs.getImages().addImage(). จากนั้นวางภาพบนสไลด์เป็น PictureFrame:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    // Add the image to the shared collection
    byte[] imageData = Files.readAllBytes(Path.of("logo.png"));
    var img = prs.getImages().addImage(imageData);

    // Place it on the slide as a PictureFrame
    var slide = prs.getSlides().get(0);
    slide.getShapes().addPictureFrame(ShapeType.RECTANGLE, 50, 50, 300, 200, img);

    prs.save("with-image.pptx", SaveFormat.PPTX);
}

อาร์กิวเมนต์ตำแหน่งสี่ตัวที่ส่งให้ addPictureFrame() คือ: x, y, width, height ในหน่วยจุด.


การเพิ่มรูปภาพจากไบต์

หากคุณมีไบต์ของภาพอยู่แล้ว (เช่น ดาวน์โหลดจาก URL หรืออ่านจากฐานข้อมูล) ให้ส่งต่อโดยตรงไปยัง addImage():

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

// Simulate having bytes in memory
byte[] imageBytes = Files.readAllBytes(Path.of("photo.jpg"));

try (Presentation prs = new Presentation()) {
    var img = prs.getImages().addImage(imageBytes);
    prs.getSlides().get(0).getShapes().addPictureFrame(ShapeType.RECTANGLE, 100, 80, 400, 250, img);
    prs.save("from-bytes.pptx", SaveFormat.PPTX);
}

การกำหนดตำแหน่งและขนาดของ PictureFrame

ตัว PictureFrame ที่ส่งกลับโดย addPictureFrame() สืบทอดคุณสมบัติทั้งหมดของ Shape คุณสมบัติทางเรขาคณิตและสามารถย้ายตำแหน่งใหม่หลังจากสร้างได้:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    byte[] imageData = Files.readAllBytes(Path.of("photo.jpg"));
    var img = prs.getImages().addImage(imageData);

    var pf = prs.getSlides().get(0).getShapes().addPictureFrame(ShapeType.RECTANGLE, 0, 0, 100, 100, img);

    // Reposition and resize after creation
    pf.setX(50);
    pf.setY(100);
    pf.setWidth(350);
    pf.setHeight(250);

    prs.save("positioned.pptx", SaveFormat.PPTX);
}

การใช้รูปภาพเป็นการเติมพื้นหลังของรูปร่าง

รูปร่างใดก็ได้ (ไม่ใช่แค่ PictureFrame) สามารถใช้ภาพเป็นการเติมพื้นหลังได้ ตั้งค่า FillType.PICTURE และกำหนดภาพให้กับ getPictureFillFormat().getPicture().setImage():

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.FillType;
import org.aspose.slides.foss.PictureFillMode;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    byte[] imageData = Files.readAllBytes(Path.of("background.png"));
    var img = prs.getImages().addImage(imageData);

    var slide = prs.getSlides().get(0);
    var shape = slide.getShapes().addAutoShape(ShapeType.ROUND_CORNER_RECTANGLE, 50, 50, 400, 250);
    shape.getFillFormat().setFillType(FillType.PICTURE);
    shape.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.STRETCH);
    shape.getFillFormat().getPictureFillFormat().getPicture().setImage(img);

    prs.save("picture-fill.pptx", SaveFormat.PPTX);
}

PictureFillMode.STRETCH ปรับขนาดภาพให้เติมเต็มรูปร่างทั้งหมด ใช้ TILE สำหรับลวดลายที่ทำซ้ำ.


การเพิ่มรูปภาพหลายรูปบนหลายสไลด์

ภาพที่เพิ่มเข้าไปใน prs.getImages() ถูกแชร์ในทุกสไลด์ทั้งหมด. เดียวกัน IPPImage วัตถุสามารถใช้ได้บนหลายสไลด์โดยไม่ต้องทำซ้ำข้อมูล:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    byte[] logoData = Files.readAllBytes(Path.of("logo.png"));
    var logo = prs.getImages().addImage(logoData);

    // Add the same image to the first slide
    prs.getSlides().get(0).getShapes().addPictureFrame(ShapeType.RECTANGLE, 600, 10, 100, 40, logo);

    prs.save("shared-image.pptx", SaveFormat.PPTX);
}

ดูเพิ่มเติม

 ภาษาไทย