Failure to Get Images of Shapes on a PowerPoint Slide Using Aspose.Slides

Hi Team,

I have a PPTX (attached) with one slide. When I try to get image of the shape on the slide, I get error from Aspose:

Processing AutoShape 1 on slide 1
Error processing shape on slide 1: Object reference not set to an instance of an object.

The code I used:

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.Animation;
using Aspose.Slides.Charts;
using Aspose.Slides.SmartArt;
using Aspose.Slides.Ink;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Xml;
class Program
{
    static void Main(string[] args)
    {
        try
        {
            // Note: Running without license (evaluation mode)
            Console.WriteLine("Running in evaluation mode");

            // Load the presentation
            using (Presentation presentation = new Presentation("sample.pptx"))
            {
                int shapeCounter = 1;

                // Loop through all slides
                for (int slideIndex = 0; slideIndex < presentation.Slides.Count; slideIndex++)
                {
                    // Get current slide
                    ISlide slide = presentation.Slides[slideIndex];

                    // Process each shape in the current slide
                    foreach (var curShape in slide.Shapes)
                    {
                        try
                        {
                            IAutoShape? autoShape = curShape as IAutoShape;
                            if (autoShape != null)
                            {
                                Console.WriteLine($"Processing AutoShape {shapeCounter} on slide {slideIndex + 1}");
                                Aspose.Slides.IImage curImage = autoShape.GetImage(0, 2, 2);

                                // Save the image with a unique name including slide number
                                string fileName = $"slide_{slideIndex + 1}_shape_{shapeCounter}.png";
                                curImage.Save(fileName, Aspose.Slides.ImageFormat.Png);
                                Console.WriteLine($"Image saved successfully as {fileName}");
                                shapeCounter++;
                            }
                            else
                            {
                                Console.WriteLine($"Skipping non-AutoShape on slide {slideIndex + 1}");
                            }
                        }
                        catch (Exception shapeEx)
                        {
                            Console.WriteLine($"Error processing shape on slide {slideIndex + 1}: {shapeEx.Message}");
                            Console.WriteLine($"This may be due to evaluation mode limitations.");
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}

I used Aspose version 25.9 and net9.0. Pls fine attached the PPT.
sample.zip (26.0 KB)

Could you pls help me resolve this issue ? Thank you,

@bhav

Internal reference: CONN-4172657

@bhav

You’re getting a NullReferenceException because the shape you try to process isn’t an IAutoShape; it’s a PictureFrame. Casting a PictureFrame to IAutoShape returns null, which leads to the error you see [2][5][6].

Handle the shapes according to their actual type

using (Presentation pres = new Presentation("sample.pptx"))
{
    int shapeCounter = 1;

    foreach (ISlide slide in pres.Slides)
    {
        foreach (IShape shape in slide.Shapes)
        {
            // AutoShape – can use GetImage
            if (shape is IAutoShape autoShape)
            {
                IImage img = autoShape.GetImage(0, 2, 2);
                img.Save($"slide_{slide.SlideNumber}_shape_{shapeCounter}.png",
                         ImageFormat.Png);
            }
            // PictureFrame – extract the embedded picture
            else if (shape is IPictureFrame picFrame)
            {
                var picture = picFrame.PictureFormat.Picture;
                if (picture?.Image != null)
                {
                    picture.Image.Save($"slide_{slide.SlideNumber}_picture_{shapeCounter}.png",
                                      ImageFormat.Png);
                }
            }
            // other shape types can be ignored or processed separately
            shapeCounter++;
        }
    }
}
  • The is IAutoShape check ensures GetImage is called only on shapes that support it.
  • For a PictureFrame, the image is obtained from PictureFormat.Picture.Image.

Additional consideration

A similar null‑reference crash has been reported when a slide contains a WMF image [3]. If your PPTX uses a WMF picture, the same issue may appear even after the type check. In that case you can:

  • Convert the WMF to a more common format (PNG/JPEG) before adding it to the presentation, or
  • Try the latest Aspose.Slides version, as newer builds may include related fixes.

If the problem remains after applying these checks, please attach the exact PPTX file again so we can investigate further.

@bhav,
Thank you for reporting the issue. I have reproduced the problem where a NullReferenceException is thrown while converting the presentation shapes to images. We apologize for any inconvenience caused.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): SLIDESNET-45299

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.