Calling shape->GetImage() in C++ Throws an ArgumentNullException

I am using Aspose.Slides for C++ 25.4 version , to convert all shapes to images. I have a ppt with math equations.

srcShape->GetImage()

Throws an exception:

System::ArgumentNullException: Value can not be null.: image

I can put checks to avoid exception like:

if (!srcShape || !srcShape->GetImage())
{
    return nullptr;
}

But Is there any way to get the thumbnail image for these math equations?

PPT attached:
Test1.pptx.zip (255.3 KB)

@pankajku

It seems that you’re encountering a System::ArgumentNullException when trying to get the image of a shape, particularly with math equations in your PowerPoint presentation using Aspose.Slides for C++. This exception indicates that the image cannot be null, suggesting that the shape you are trying to access may not support image generation directly.

To work around this issue, you can generate a thumbnail image for the shape instead. Aspose.Slides allows you to create thumbnails for shapes, which can be a suitable alternative for visual representation of shapes that do not have an image.

Here’s a simple example on how to create a thumbnail for a shape:

auto presentation = MakeObject<Presentation>(u"YourPresentation.pptx");
auto shape = presentation->get_Slide(0)->get_Shape(0); // Get the desired shape

// Generate a thumbnail image with default scaling
auto image = shape->GetImage(); // This may throw an exception for some shapes

// If the above fails, you can try generating a thumbnail with defined bounds
auto bounds = ShapeThumbnailBounds::Appearance; // or ShapeThumbnailBounds::Shape
auto scale = 1; // Scaling factor

image = shape->GetImage(bounds, scale, scale);
image->Save(u"Shape_thumbnail_out.png", ImageFormat::Png);
image->Dispose();
presentation->Dispose();

In this example, you first attempt to get the image. If it fails, you can utilize the GetImage method with specific bounds and scaling to create a thumbnail of the shape, which should work even for shapes that do not generate direct images.

Make sure that you adjust the ShapeThumbnailBounds based on your requirement to either capture the shape’s appearance or its bounds. This should help you retrieve a visual representation of your math equations without running into null exceptions.

If you need further assistance, feel free to ask!

Sources:
[1]: Shape Thumbnails|Aspose.Slides Documentation

Tried the following code. Still not able to get thumbnail image.

System::SharedPtr<IImage> image;
try {
    // First try to get image directly
    if (srcShape) {
        image = srcShape->GetImage();
    }
}
catch (const std::exception& e) {

}

// If direct GetImage() failed or returned null, try with ShapeThumbnailBounds
if (!image && srcShape) {
    try {
        auto bounds = Aspose::Slides::ShapeThumbnailBounds::Appearance;
        auto scale = 1; // Scaling factor
        image = srcShape->GetImage(bounds, scale, scale);
    }
    catch (const std::exception& e) {

        return nullptr;
    }
}

if (!image) {

    return nullptr;
}

@pankajku,
Thank you for reporting on the issue. I’ve reproduced the problem with appearing the ArgumentNullException when using the following code snippet:

auto bounds = Aspose::Slides::ShapeThumbnailBounds::Appearance;
auto scale = 1;
image = srcShape->GetImage(bounds, scale, scale);

We are sorry that you encountered this problem.

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): SLIDESCPP-4038

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.

@pankajku,
The issues you found earlier (filed as SLIDESCPP-4038) have been resolved in Aspose.Slides for C++ 25.8 (NuGet (x64), NuGet (x86), Windows, Linux, macOS).
You can check all fixes on the Release Notes page.
You can also find the latest version of our library on the Product Download page.

Hi @andrey.potapov ,

After upgrading to latest Aspose version, I am now getting this exception while trying to access thumbnail image using above mentioned APIs:

System::NullReferenceException: Object reference not set to an instance of an object.

Thanks

Another exception found for a diff math equation in mac:

System::Collections::Generic::KeyNotFoundException: The given key was not present in the dictionary.

PFA ppt file

_Test.pptx.zip (265.5 KB)

@pankajku,
Could you please clarify whether the NullReferenceException also occurs on macOS and whether the presentation file from your first post was used?

NullReferenceException occurs on macOS. I have tried with both mentioned ppt files

Another finding is that while trying to get thumbnail image of entire slide, same math equation does not come with slide thumbnail.

auto slideImage = mSrcSlide->GetImage(1.0f, 1.0f);

try {
        shapeImage = srcShape->GetImage();
    }
    catch (const std::exception& e) {
        CPLOG_DEBUG("[PPT] - GetImage() failed for shape {0} in slide {1}: {2}",
            srcShape->get_UniqueId(), mSrcSlide->get_SlideNumber(), e.what());
    }
    catch (...) {
        CPLOG_DEBUG("[PPT] - GetImage() failed for shape {0} in slide {1}",
            srcShape->get_UniqueId(), mSrcSlide->get_SlideNumber());
    }

    // Fallback: try GetImage with ShapeThumbnailBounds::Appearance
    if (!shapeImage) {
        try {
            shapeImage = srcShape->GetImage(ShapeThumbnailBounds::Appearance, 1.0f, 1.0f);
        }
        catch (const std::exception& e) {
            CPLOG_DEBUG("[PPT] - GetImage(Appearance) failed for shape {0} in slide {1}: {2}",
                srcShape->get_UniqueId(), mSrcSlide->get_SlideNumber(), e.what());
        }
        catch (...) {
            CPLOG_DEBUG("[PPT] - GetImage(Appearance) failed for shape {0} in slide {1}",
                srcShape->get_UniqueId(), mSrcSlide->get_SlideNumber());
        }
    }

    // Fallback: try GetImage with ShapeThumbnailBounds::Slide
    if (!shapeImage) {
        try {
            shapeImage = srcShape->GetImage(ShapeThumbnailBounds::Slide, 1.0f, 1.0f);
        }
        catch (const std::exception& e) {
            CPLOG_DEBUG("[PPT] - GetImage(Slide) failed for shape {0} in slide {1}: {2}",
                srcShape->get_UniqueId(), mSrcSlide->get_SlideNumber(), e.what());
        }
        catch (...) {
            CPLOG_DEBUG("[PPT] - GetImage(Slide) failed for shape {0} in slide {1}",
                srcShape->get_UniqueId(), mSrcSlide->get_SlideNumber());
        }
    }

@pankajku,
Thank you for the additional information. Could you please also specify the versions of macOS and Aspose.Slides that you are using?

macOS version - 15.6.1
Aspose.slides 25.12

Please note that this issue is not observed on windows with same aspose version.

@pankajku,
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): 
  SLIDESCPP-4107 - IShape::GetImage throws a NullReferenceException
  SLIDESCPP-4108 - IShape::GetImage throws a KeyNotFoundException

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.