Working with PowerPoint Images

28 Jul 202619 minutes to read

Adding Images

Essential® Presentation library facilitates adding or modifying the images in a PowerPoint Presentation. The following code example demonstrates how to add a new image to the presentation.

//Create an instance of Presentation
IPresentation pptxDoc = Presentation.Create();
//Add a blank slide.
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Get a picture as a stream.
FileStream pictureStream = new FileStream("Image.png", FileMode.Open);
//Add the picture to a slide by specifying its size and position.
IPicture picture = slide.Pictures.AddPicture(pictureStream, 0, 0, 250, 250);
//Save the PowerPoint Presentation to a file
pptxDoc.Save("Sample.pptx");
//Dispose the image stream
pictureStream.Dispose();
//Close the Presentation
pptxDoc.Close();
//Create an instance of Presentation
IPresentation pptxDoc = Presentation.Create();
//Add a blank slide.
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Get a picture as a stream.
Stream pictureStream = File.Open("Image.png", FileMode.Open);
//Add the picture to a slide by specifying its size and position.
IPicture picture = slide.Pictures.AddPicture(pictureStream, 0, 0, 250, 250);
//Save the Presentation to the file system.
pptxDoc.Save("Sample.pptx");
//Dispose the image stream
pictureStream.Dispose();
//Close the Presentation
pptxDoc.Close();
'Create an instance of Presentation
Dim pptxDoc As IPresentation = Presentation.Create()
'Add a blank slide.
Dim slide As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Blank)
'Get a picture as a stream.
Dim pictureStream As Stream = File.Open("Image.png", FileMode.Open)
'Add the picture to a slide by specifying its size and position.
Dim picture As IPicture = slide.Pictures.AddPicture(pictureStream, 0, 0, 250, 250)
'Save the Presentation to the file system.
pptxDoc.Save("Sample.pptx")
'Dispose the image stream
pictureStream.Dispose()
'Close the Presentation
pptxDoc.Close()

You can download a complete working sample from GitHub.

Replacing Images

The following code example demonstrates how to replace an existing image in a slide. The IPicture.ImageData property accepts a byte array that represents the new image data.

//Open an existing PowerPoint Presentation
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Retrieve the first slide from the Presentation.
ISlide slide = pptxDoc.Slides[0];
//Retrieve the first picture from the slide.
IPicture picture = slide.Pictures[0];
//Get the new picture as a stream.
FileStream pictureStream = new FileStream("Image.png", FileMode.Open);
//Create an instance for memory stream
MemoryStream memoryStream = new MemoryStream();
//Copy stream to memoryStream.
pictureStream.CopyTo(memoryStream);
//Replace the existing image with the new image.
picture.ImageData = memoryStream.ToArray();
//Save the PowerPoint Presentation to a file
pptxDoc.Save("Output.pptx");
//Dispose the streams
pictureStream.Dispose();
memoryStream.Dispose();
//Close the Presentation
pptxDoc.Close();
//Open an existing Presentation.
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Retrieve the first slide from the Presentation.
ISlide slide = pptxDoc.Slides[0];
//Retrieve the first picture from the slide.
IPicture picture = slide.Pictures[0];
//Get the new picture as a stream.
Stream pictureStream = File.Open("Image.png", FileMode.Open);
//Create an instance for memory stream
MemoryStream memoryStream = new MemoryStream();
//Copy stream to memoryStream.
pictureStream.CopyTo(memoryStream);
//Replace the existing image with the new image.
picture.ImageData = memoryStream.ToArray();
//Save the Presentation to the file system.
pptxDoc.Save("Output.pptx");
//Dispose the streams
pictureStream.Dispose();
memoryStream.Dispose();
//Close the Presentation
pptxDoc.Close();
'Open an existing Presentation.
Dim pptxDoc As IPresentation = Presentation.Open("Sample.pptx")
'Retrieve the first slide from the Presentation.
Dim slide As ISlide = pptxDoc.Slides(0)
'Retrieve the first picture from the slide.
Dim picture As IPicture = slide.Pictures(0)
'Get the new picture as a stream.
Dim pictureStream As Stream = File.Open("Image.png", FileMode.Open)
'Create an instance for memory stream
Dim memoryStream As New MemoryStream()
'Copy stream to memoryStream.
pictureStream.CopyTo(memoryStream)
'Replace the existing image with the new image.
picture.ImageData = memoryStream.ToArray()
'Save the Presentation to the file system.
pptxDoc.Save("Output.pptx")
'Dispose the streams
pictureStream.Dispose()
memoryStream.Dispose()
'Close the Presentation
pptxDoc.Close()

You can download a complete working sample from GitHub.

Adding SVG Images

SVG images can be inserted in a PowerPoint slide for displaying images with accuracy when scaling or zooming the page. The Essential® Presentation library allows you to insert and edit an SVG image along with its fallback image.

The following code example demonstrates how to add a new SVG image into a slide.

//Create an instance of Presentation
IPresentation pptxDoc = Presentation.Create();
//Add a blank slide
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Get an SVG image (icon) as a stream
FileStream svgImageStream = new FileStream("Image.svg", FileMode.Open);
//Get a fallback image as a stream
FileStream fallbackImageStream = new FileStream("Image.png", FileMode.Open);
//Add the icon to a slide by specifying its size and position
IPicture icon = slide.Pictures.AddPicture(svgImageStream, fallbackImageStream, 0, 0, 250, 250);
//Save the PowerPoint Presentation to a file
pptxDoc.Save("Sample.pptx");
//Dispose the fallback image stream
fallbackImageStream.Dispose();
//Dispose the SVG image stream
svgImageStream.Dispose();
//Close the Presentation
pptxDoc.Close();
//Create an instance of Presentation
IPresentation pptxDoc = Presentation.Create();
//Add a blank slide
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Get an SVG image (icon) as a stream
Stream svgImageStream = File.Open("Image.svg", FileMode.Open);
//Get a fallback image as a stream
Stream fallbackImageStream = File.Open("Image.png", FileMode.Open);
//Add the icon to a slide by specifying its size and position
IPicture icon = slide.Pictures.AddPicture(svgImageStream, fallbackImageStream, 0, 0, 250, 250);
//Save the Presentation to the file system
pptxDoc.Save("Sample.pptx")
//Dispose the fallback image stream
fallbackImageStream.Dispose()
//Dispose the SVG image stream
svgImageStream.Dispose()
//Close the Presentation
pptxDoc.Close()
'Create an instance of Presentation
Dim pptxDoc As IPresentation = Presentation.Create()
'Add a blank slide
Dim slide As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Blank)
'Get an SVG image (icon) as a stream
Dim svgImageStream As Stream = File.Open("Image.svg", FileMode.Open)
'Get a fallback image as a stream
Dim fallbackImageStream As Stream = File.Open("Image.png", FileMode.Open)
'Add the icon to a slide by specifying its size and position
Dim icon As IPicture = slide.Pictures.AddPicture(svgImageStream, fallbackImageStream, 0, 0, 250, 250)
'Save the Presentation to the file system
pptxDoc.Save("Sample.pptx")
'Dispose the fallback image stream
fallbackImageStream.Dispose()
'Dispose the SVG image stream
svgImageStream.Dispose()
'Close the Presentation
pptxDoc.Close()

You can download a complete working sample from GitHub.

Replacing SVG Images

The following code example demonstrates how to replace an existing SVG image in a slide.

//Open an existing PowerPoint Presentation
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Retrieve the first slide from the Presentation
ISlide slide = pptxDoc.Slides[0];
//Retrieve the icon object from the slide
IPicture icon = slide.Pictures[0];
//Get the new picture as a stream
FileStream pictureStream = new FileStream("Image.svg", FileMode.Open);
//Create an instance for memory stream
MemoryStream memoryStream = new MemoryStream();
//Copy stream to memoryStream
pictureStream.CopyTo(memoryStream);
//Replace the existing icon image with the new image
//SvgData property will return null if it is not an icon
icon.SvgData = memoryStream.ToArray();
//Save the PowerPoint Presentation to a file
pptxDoc.Save("Output.pptx");
//Dispose the streams
pictureStream.Dispose();
memoryStream.Dispose();
//Close the Presentation
pptxDoc.Close();
//Open an existing Presentation
IPresentation pptxDoc = Presentation.Open("Sample.pptx")
//Retrieve the first slide from the Presentation
ISlide slide = pptxDoc.Slides[0]
//Retrieve the icon object from the slide
IPicture icon = slide.Pictures[0]
//Get the new picture as a stream
Stream pictureStream = File.Open("Image.svg", FileMode.Open)
//Create an instance for memory stream
MemoryStream memoryStream = new MemoryStream()
//Copy stream to memoryStream
pictureStream.CopyTo(memoryStream)
//Replace the existing icon image with the new image
'SvgData property will return null if it is not an icon
icon.SvgData = memoryStream.ToArray()
//Save the Presentation to the file system
pptxDoc.Save("Output.pptx")
//Dispose the streams
pictureStream.Dispose()
memoryStream.Dispose()
//Close the Presentation
pptxDoc.Close()
'Open an existing Presentation.
Dim pptxDoc As IPresentation = Presentation.Open("Sample.pptx")
'Retrieve the first slide from the Presentation
Dim slide As ISlide = pptxDoc.Slides(0)
'Retrieve the icon object from the slide
Dim icon As IPicture = slide.Pictures(0)
'Get the new picture as a stream
Dim pictureStream As Stream = File.Open("Image.svg", FileMode.Open)
'Create an instance for memory stream
Dim memoryStream As New MemoryStream()
'Copy stream to memoryStream
pictureStream.CopyTo(memoryStream)
'Replace the existing icon image with the new image
'SvgData property will return null if it is not an icon
icon.SvgData = memoryStream.ToArray()
'Save the Presentation to the file system
pptxDoc.Save("Output.pptx")
'Dispose the streams
pictureStream.Dispose()
memoryStream.Dispose()
'Close the Presentation
pptxDoc.Close()

You can download a complete working sample from GitHub.

Crop Image

Crop the images in an existing presentation or insert the cropped picture while creating a presentation from scratch by applying crop properties using the .NET PowerPoint Library.

The following code example demonstrates how to crop an image in a PowerPoint slide.

//Open an existing PowerPoint Presentation.
using (IPresentation pptxDoc = Presentation.Open("Sample.pptx"))
{
    //Retrieve the first slide from the Presentation.
    ISlide slide = pptxDoc.Slides[0];
    //Retrieve the first picture from the slide.
    IPicture picture = slide.Pictures[0];

    //Apply bounding box size and position.
    picture.Crop.ContainerWidth = 114.48f;
    picture.Crop.ContainerHeight = 56.88f;
    picture.Crop.ContainerLeft = 94.32f;
    picture.Crop.ContainerTop = 128.16f;

    //Apply cropping size and offsets.
    picture.Crop.Width = 900.72f;
    picture.Crop.Height = 74.88f;
    picture.Crop.OffsetX = 329.04f;
    picture.Crop.OffsetY = -9.36f;

    //Save the PowerPoint Presentation to a file.
    pptxDoc.Save("Output.pptx");
}
//Open an existing PowerPoint Presentation.
using (IPresentation pptxDoc = Presentation.Open("Sample.pptx"))
{
    //Retrieve the first slide from the Presentation.
    ISlide slide = pptxDoc.Slides[0];
    //Retrieve the first picture from the slide.
    IPicture picture = slide.Pictures[0];

    //Apply bounding box size and position.
    picture.Crop.ContainerWidth = 114.48f;
    picture.Crop.ContainerHeight = 56.88f;
    picture.Crop.ContainerLeft = 94.32f;
    picture.Crop.ContainerTop = 128.16f;

    //Apply cropping size and offsets.
    picture.Crop.Width = 900.72f;
    picture.Crop.Height = 74.88f;
    picture.Crop.OffsetX = 329.04f;
    picture.Crop.OffsetY = -9.36f;

    // Save the PowerPoint Presentation.
    pptxDoc.Save("Output.pptx");
}
' Open an existing PowerPoint Presentation.
Using pptxDoc As IPresentation = Presentation.Open("Sample.pptx")
    ' Retrieve the first slide from the Presentation.
    Dim slide As ISlide = pptxDoc.Slides(0)
    ' Retrieve the first picture from the slide.
    Dim picture As IPicture = slide.Pictures(0)

    ' Apply bounding box size and position
    picture.Crop.ContainerWidth = 114.48F
    picture.Crop.ContainerHeight = 56.88F
    picture.Crop.ContainerLeft = 94.32F
    picture.Crop.ContainerTop = 128.16F

    ' Apply cropping size and offsets
    picture.Crop.Width = 900.72F
    picture.Crop.Height = 74.88F
    picture.Crop.OffsetX = 329.04F
    picture.Crop.OffsetY = -9.36F

    ' Save the PowerPoint Presentation.
    pptxDoc.Save("Output.pptx")
End Using

NOTE

The bounding box properties (ContainerLeft, ContainerTop, ContainerWidth, ContainerHeight) must be set before applying the cropping properties for proper functionality.

You can download a complete working sample from GitHub.

Removing Images

The following code example demonstrates how to remove an existing image in a PowerPoint slide.

//Open an existing PowerPoint Presentation
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Retrieve the first slide from the Presentation
ISlide slide = pptxDoc.Slides[0];
//Iterate through a copy of the pictures collection and remove each picture.
foreach (IPicture picture in slide.Pictures)
{
    //Remove the picture from the slide.
    slide.Pictures.Remove(picture);
}
//Save the PowerPoint Presentation to a file
pptxDoc.Save("Output.pptx");
//Close the Presentation
pptxDoc.Close();
//Open an existing Presentation from the file system.
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Retrieve the first slide from the Presentation
ISlide slide = pptxDoc.Slides[0];
//Iterate through a copy of the pictures collection and remove each picture.
foreach (IPicture picture in slide.Pictures)
{
    //Remove the picture from the slide.
    slide.Pictures.Remove(picture);
}
//Save the Presentation to the file system.
pptxDoc.Save("Output.pptx");
//Close the Presentation
pptxDoc.Close();
'Open an existing Presentation from the file system.
Dim pptxDoc As IPresentation = Presentation.Open("Sample.pptx")
'Retrieve the first slide from the Presentation
Dim slide As ISlide = pptxDoc.Slides(0)
'Iterate through a copy of the pictures collection and remove each picture.
Dim pictures As List(Of IPicture) = slide.Pictures.Cast(Of IPicture)().ToList()
For Each picture As IPicture In pictures
    'Remove the picture from the slide.
    slide.Pictures.Remove(picture)
Exit For
Next
'Save the Presentation to the file system.
pptxDoc.Save("Output.pptx")
'Close the Presentation
pptxDoc.Close()

You can download a complete working sample from GitHub.

Online Demo

  • Explore how to add and format images in a PowerPoint presentation using the .NET PowerPoint Library (Presentation) in a live demo here.

See Also