การทำงานกับรูปภาพในงานนำเสนอ — Aspose.Slides FOSS สำหรับ .NET
Aspose.Slides FOSS for .NET lets you embed images in a presentation’s shared image collection and display them on slides using PictureFrame รูปร่าง. ภาพยังสามารถใช้เป็นการเติมพื้นหลังของรูปร่างผ่าน FillType.Picture.
การเพิ่มรูปภาพจากไฟล์
โหลดไบต์ของภาพจากดิสก์และเพิ่มลงในคอลเลกชันภาพของงานนำเสนอด้วย prs.Images.AddImage(). จากนั้นวางภาพบนสไลด์เป็น PictureFrame:
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
// Add the image to the shared collection
var imageData = File.ReadAllBytes("logo.png");
var img = prs.Images.AddImage(imageData);
// Place it on the slide as a PictureFrame
var slide = prs.Slides[0];
slide.Shapes.AddPictureFrame(ShapeType.Rectangle, 50, 50, 300, 200, img);
prs.Save("with-image.pptx", SaveFormat.Pptx);อาร์กิวเมนต์ตำแหน่งสี่ตัวที่ส่งให้ AddPictureFrame() คือ: x, y, width, height ในหน่วยจุด.
การเพิ่มรูปภาพจากสตรีม
หากคุณมีข้อมูลภาพจากสตรีม (เช่น ดาวน์โหลดจาก URL หรืออ่านจากฐานข้อมูล) ให้ส่งโดยตรงไปยัง AddImage():
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
using var stream = File.OpenRead("photo.jpg");
var img = prs.Images.AddImage(stream);
prs.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 100, 80, 400, 250, img);
prs.Save("from-stream.pptx", SaveFormat.Pptx);การกำหนดตำแหน่งและขนาดของ PictureFrame
ค่า PictureFrame ที่ส่งกลับโดย AddPictureFrame() สืบทอดคุณสมบัติทั้งหมด Shape ของเรขาคณิตและสามารถย้ายตำแหน่งใหม่หลังจากสร้างแล้ว:
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var imageData = File.ReadAllBytes("photo.jpg");
var img = prs.Images.AddImage(imageData);
var pf = prs.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 0, 0, 100, 100, img);
// Reposition and resize after creation
pf.X = 50;
pf.Y = 100;
pf.Width = 350;
pf.Height = 250;
prs.Save("positioned.pptx", SaveFormat.Pptx);การใช้รูปภาพเป็นการเติมรูปทรง
รูปร่างใดก็ได้ (ไม่ใช่แค่ PictureFrame) สามารถใช้ภาพเป็นการเติมพื้นหลังได้ ตั้งค่า FillType = FillType.Picture และกำหนดภาพให้กับ PictureFillFormat.Picture.Image:
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var imageData = File.ReadAllBytes("background.png");
var img = prs.Images.AddImage(imageData);
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.RoundCornerRectangle, 50, 50, 400, 250);
shape.FillFormat.FillType = FillType.Picture;
shape.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;
shape.FillFormat.PictureFillFormat.Picture.Image = img;
prs.Save("picture-fill.pptx", SaveFormat.Pptx);PictureFillMode.Stretch ปรับขนาดภาพให้เติมเต็มรูปร่างทั้งหมด.
การเพิ่มรูปภาพหลายรูปบนหลายสไลด์
ภาพที่เพิ่มลงใน prs.Images จะถูกแชร์ระหว่างสไลด์ทั้งหมด วัตถุภาพเดียวกันสามารถใช้บนหลายสไลด์ได้โดยไม่ต้องทำซ้ำข้อมูล:
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var logoData = File.ReadAllBytes("logo.png");
var logo = prs.Images.AddImage(logoData);
// Add the same image to the first slide
prs.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 600, 10, 100, 40, logo);
prs.Save("shared-image.pptx", SaveFormat.Pptx);