Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"A generic error occurred in GDI " in outlook-addin

Our product is an addin for Microsoft Office and we have some GUI in it.

We use Graphics.DrawImage to draw images.

The problem is that we have a specific user that reported some issues with GUI not showing up in Microsoft Outlook, but in the same time it works fine in Microfot Word.

The error I see in the logs is:

System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
    at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
    at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)

I know that may imply on some kind of limited resources problem, but the thing is that the same GUI is running in Microsoft Word perfectly fine. Furthermore, the image that we try to draw is a 16x16 tiny icon.

Is there any kind of limitations that a specific process can have that might cause this?

Update: The issue occurred with a specific image that was not loaded properly from a file stream.

like image 534
ShaLi Shaltiel Avatar asked Jan 31 '26 09:01

ShaLi Shaltiel


1 Answers

When using a MemoryStream it's important that the stream is kept alive during all usage of the image until the image is disposed. This is because the image needs to read sometimes from the memory area represented by the memory stream.

If the stream is closed before the image is disposed the memory area may or may not become invalid.

When you dispose the image the stream will be closed as well.