How to Add Images to PowerPoint Slides in .NET

How to Add Images to PowerPoint Slides in .NET

This guide shows how to embed images into PowerPoint slides using Aspose.Slides FOSS for .NET. Load image data into prs.Images.AddImage(), then call slide.Shapes.AddPictureFrame() to position the image on the slide. All dimensions are in points (1 point = 1/72 inch).

Step 1: Install the Package

Add the NuGet package to your project by running the dotnet CLI install command:

dotnet add package Aspose.Slides.Foss

Step 2: Add an Image from a File

Load image bytes from a file into prs.Images.AddImage(), then call slide.Shapes.AddPictureFrame() with the target position and size in points:

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 method signature accepts shape type, position, dimensions, and image reference:

AddPictureFrame(shapeType, x, y, width, height, image) -> PictureFrame

<button class=“hextra-code-copy-btn hx-group/copybtn hx-transition-all active:hx-opacity-50 hx-bg-primary-700/5 hx-border hx-border-black/5 hx-text-gray-600 hover:hx-text-gray-900 hx-rounded-md hx-p-1.5 dark:hx-bg-primary-300/10 dark:hx-border-white/10 dark:hx-text-gray-400 dark:hover:hx-text-gray-50” title=“Copy code”

<div class="copy-icon group-[.copied]/copybtn:hx-hidden hx-pointer-events-none hx-h-4 hx-w-4"></div>
<div class="success-icon hx-hidden group-[.copied]/copybtn:hx-block hx-pointer-events-none hx-h-4 hx-w-4"></div>

All 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.


Step 3: 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);

Step 4: 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);
PictureFillModeBehaviour
StretchScale the image to fill the frame, ignoring aspect ratio
TileRepeat the image as a tiled pattern

Step 5: Add Multiple Images to Different Slides

Add a picture frame to each slide by iterating over a list of image file paths and calling AddPictureFrame() on each slide in the collection:

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

Step 6: Count Images in an Existing Presentation

Load an existing presentation and read prs.Images.Count to get the total number of images stored in the shared collection:

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.

See Also

 English