docs icon indicating copy to clipboard operation
docs copied to clipboard

[Breaking change]: System.Drawing OutOfMemoryException changed to ExternalException

Open JeremyKuhne opened this issue 7 months ago • 1 comments

Description

GDI+ isn't particularly good at returning errors when it is unable to create internal objects. There are many cases where object creation will fail due to invalid input and higher-level code will get a null and turn it into Status.OutOfMemory.

As this is a frequent cause of confusion, we're changing this to ExternalException, which is already thrown in other code paths. We had already changed the exception message.

Version

.NET 10 Preview 5

Previous behavior

https://learn.microsoft.com/windows/win32/api/gdiplustypes/ne-gdiplustypes-status GDI+ errors were raised as System.OutOfMemoryException.

New behavior

https://learn.microsoft.com/windows/win32/api/gdiplustypes/ne-gdiplustypes-status GDI+ errors are raised as System.Runtime.InteropServices.ExternalException.

Type of breaking change

  • [ ] Binary incompatible: Existing binaries might encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
  • [ ] Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code might require source changes to compile successfully.
  • [x] Behavioral change: Existing binaries might behave differently at run time.

Reason for change

GDI+ isn't particularly good at returning errors when it is unable to create internal objects. There are many cases where object creation will fail due to invalid input and higher-level code will get a null and turn it into Status.OutOfMemory.

As this is a frequent cause of confusion, we're changing this to ExternalException, which is already thrown in other code paths. We had already changed the exception message.

Recommended action

If catching OutOfMemoryException in code that uses System.Drawing, ensure you're also catching ExternalException.

Feature area

Windows Forms

Affected APIs

No response


Associated WorkItem - 439066

JeremyKuhne avatar May 08 '25 20:05 JeremyKuhne

@adegeo This should be good to assign to Copilot.

gewarren avatar Jul 25 '25 18:07 gewarren