Working with Images in Presentations — Aspose.Slides FOSS for .NET

Aspose.Slides FOSS for .NET lets you embed images in a presentation’s shared image collection and display them on slides using PictureFrame shapes. Images can also be used as shape background fills via FillType.Picture.


Adding an Image from File

Load image bytes from disk and add them to the presentation’s image collection with prs.Images.AddImage(). Then place the image on a slide as a 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);

The four positional arguments to AddPictureFrame() are: x, y, width, height in points.


Adding an Image from a Stream

If you have image data from a stream (e.g., downloaded from a URL or read from a database), pass it directly to 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);

Positioning and Sizing a PictureFrame

The PictureFrame returned by AddPictureFrame() inherits all Shape geometry properties and can be repositioned after creation:

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);

Using an Image as a Shape Fill

Any shape (not just PictureFrame) can use an image as its background fill. Set FillType = FillType.Picture and assign the image to 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 scales the image to fill the entire shape.


Adding Multiple Images Across Slides

Images added to prs.Images are shared across all slides. The same image object can be used on multiple slides without duplicating the data:

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);

See Also