It is pretty easy in MS PowerPoint to export a certain shape as image with following two steps:
- 1. Select the object shape
- 2. From the right-click menu select Save as Picture
However, Spire.Presentation also provides easy method for programmers to save shapes out of slides. In the following section, we’ll introduce how to export shapes as images via Spire.Presentation with an example.
Test File:
As is shown in the screenshot, the sample file for testing contains several shapes on the first slide.
Code Snippet for Exporting Shape as Image:
Step 1: Initialize a new instance of Presentation class and load the test file from disk.
Presentation ppt = new Presentation(); ppt.LoadFromFile("test.pptx");
Step 2: Use for loop to traverse every shape in the slide. Call ShapeList.SaveAsImage(int shapeIndex) to save shape as image, then save this image to the specified file in the specified format.
for (int i = 0; i < ppt.Slides[0].Shapes.Count; i++) { Image image = ppt.Slides[0].Shapes.SaveAsImage(i); image.Save(String.Format("Picture-{0}.png", i), System.Drawing.Imaging.ImageFormat.Png); }
Output:
Picture-0
Picture-1
Entire Code:
C#
using Spire.Presentation; using System.Drawing; namespace ExportShape { class Program { static void Main(string[] args) { //create PPT document Presentation ppt = new Presentation(); ppt.LoadFromFile("test.pptx"); for (int i = 0; i < ppt.Slides[0].Shapes.Count; i++) { Image image = ppt.Slides[0].Shapes.SaveAsImage(i); image.Save(System.String.Format("Picture-{0}.png", i), System.Drawing.Imaging.ImageFormat.Png); } } } }
VB.NET
Imports Spire.Presentation Imports System.Drawing Namespace ExportShape Class Program Private Shared Sub Main(args As String()) 'create PPT document Dim ppt As New Presentation() ppt.LoadFromFile("test.pptx") For i As Integer = 0 To ppt.Slides(0).Shapes.Count - 1 Dim image As Image = ppt.Slides(0).Shapes.SaveAsImage(i) image.Save(System.[String].Format("Picture-{0}.png", i), System.Drawing.Imaging.ImageFormat.Png) Next End Sub End Class End Namespace