Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
GDI+ error handling in System.Drawing has been updated to throw ExternalException instead of OutOfMemoryException for Status.OutOfMemory
errors.
Version introduced
.NET 10 Preview 5
Previous behavior
When GDI+ encountered Status.OutOfMemory
errors (often due to invalid input rather than actual memory issues), System.Drawing APIs threw OutOfMemoryException.
New behavior
Starting in .NET 10, when GDI+ encounters Status.OutOfMemory
errors, System.Drawing APIs now throw ExternalException.
Type of breaking change
This is a behavioral change.
Reason for change
GDI+ isn't particularly good at returning errors when it's unable to create internal objects. There are many cases where object creation fails due to invalid input, and higher-level code gets a null and turns it into Status.OutOfMemory
. This is frequently a source of confusion since the error is often not related to actual memory issues.
The change to ExternalException
provides more accurate error reporting, as this exception type is already thrown in other System.Drawing code paths for similar GDI+ errors.
Recommended action
If your code catches OutOfMemoryException
when using System.Drawing APIs, ensure you also catch ExternalException
to handle these GDI+ errors.
try
{
// System.Drawing operations
}
catch (ExternalException ex)
{
// Handle GDI+ errors (including former OutOfMemoryException cases)
}
catch (OutOfMemoryException ex)
{
// Handle actual memory issues
}
Try
' System.Drawing operations
Catch ex As ExternalException
' Handle GDI+ errors (including former OutOfMemoryException cases)
Catch ex As OutOfMemoryException
' Handle actual memory issues
End Try
Affected APIs
All System.Drawing APIs that interact with GDI+ and previously could throw OutOfMemoryException
for Status.OutOfMemory
errors, including but not limited to:
- System.Drawing.Bitmap constructors and methods
- System.Drawing.Graphics methods
- System.Drawing.Image methods
- System.Drawing.Icon constructors and methods
- Other System.Drawing types that use GDI+ internally