How to Add Images to PowerPoint Slides in .NET
Images in Aspose.Slides FOSS are embedded as picture frames, shapes that hold an image and can be positioned, resized, and styled like any other shape. The image data is stored once in the prs.Images collection and referenced by the frame.
Prerequisites
dotnet add package Aspose.Slides.FossAdd an Image from a File
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
// Load image into the presentation's image collection
byte[] imageData = File.ReadAllBytes("photo.jpg");
var img = prs.Images.AddImage(imageData);
// Add a picture frame at (x=50, y=50, width=400, height=300) in points
var frame = slide.Shapes.AddPictureFrame(
ShapeType.Rectangle,
50, 50, 400, 300,
img
);
prs.Save("with-image.pptx", SaveFormat.Pptx);The AddPictureFrame signature:
AddPictureFrame(shapeType, x, y, width, height, image) -> PictureFrameAll dimensions are in points (1 point = 1/72 inch). For a standard 10 x 7.5 inch slide the coordinate space is 720 x 540 points.
Add an Image from a Stream
If you already have the image as a stream (e.g., downloaded from a URL or read from a database):
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
using var stream = File.OpenRead("logo.png");
var img = prs.Images.AddImage(stream);
prs.Slides[0].Shapes.AddPictureFrame(
ShapeType.Rectangle,
200, 100, 300, 200,
img
);
prs.Save("logo-slide.pptx", SaveFormat.Pptx);Control the Fill Mode
The PictureFormat on a PictureFrame controls how the image fills the frame bounds:
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
byte[] texData = File.ReadAllBytes("texture.png");
var img = prs.Images.AddImage(texData);
var frame = prs.Slides[0].Shapes.AddPictureFrame(
ShapeType.Rectangle, 50, 50, 600, 350, img
);
// STRETCH: scale image to fill the frame exactly (default)
frame.PictureFormat.PictureFillMode = PictureFillMode.Stretch;
// TILE: repeat the image in a grid pattern
// frame.PictureFormat.PictureFillMode = PictureFillMode.Tile;
prs.Save("filled.pptx", SaveFormat.Pptx);PictureFillMode | Behaviour |
|---|---|
Stretch | Scale the image to fill the frame, ignoring aspect ratio |
Tile | Repeat the image as a tiled pattern |
Add Multiple Images to Different Slides
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
string[] imageFiles = { "slide1.jpg", "slide2.jpg", "slide3.jpg" };
using var prs = new Presentation();
var layout = prs.Slides[0].LayoutSlide;
// Ensure enough slides exist
while (prs.Slides.Count < imageFiles.Length)
prs.Slides.AddEmptySlide(layout);
for (int i = 0; i < imageFiles.Length; i++)
{
if (!File.Exists(imageFiles[i])) continue;
var img = prs.Images.AddImage(File.ReadAllBytes(imageFiles[i]));
prs.Slides[i].Shapes.AddPictureFrame(
ShapeType.Rectangle, 0, 0, 720, 540, img
);
}
prs.Save("multi-image.pptx", SaveFormat.Pptx);Count Images in an Existing Presentation
using Aspose.Slides.Foss;
using var prs = new Presentation("with-image.pptx");
Console.WriteLine($"Presentation contains {prs.Images.Count} image(s)");The prs.Images collection is shared across all slides: the same image bytes are stored once even if the picture frame appears on multiple slides.