|
| 1 | +--- |
| 2 | +title: "Breaking change: System.Drawing OutOfMemoryException changed to ExternalException" |
| 3 | +description: "Learn about the breaking change in .NET 10 where System.Drawing GDI+ OutOfMemory errors now throw ExternalException instead of OutOfMemoryException." |
| 4 | +ms.date: 08/15/2025 |
| 5 | +ai-usage: ai-assisted |
| 6 | +--- |
| 7 | + |
| 8 | +# System.Drawing OutOfMemoryException changed to ExternalException |
| 9 | + |
| 10 | +GDI+ error handling in System.Drawing has been updated to throw <xref:System.Runtime.InteropServices.ExternalException> instead of <xref:System.OutOfMemoryException> for `Status.OutOfMemory` errors. |
| 11 | + |
| 12 | +## Version introduced |
| 13 | + |
| 14 | +.NET 10 Preview 5 |
| 15 | + |
| 16 | +## Previous behavior |
| 17 | + |
| 18 | +When GDI+ encountered `Status.OutOfMemory` errors (often due to invalid input rather than actual memory issues), System.Drawing APIs threw <xref:System.OutOfMemoryException>. |
| 19 | + |
| 20 | +## New behavior |
| 21 | + |
| 22 | +When GDI+ encounters `Status.OutOfMemory` errors, System.Drawing APIs now throw <xref:System.Runtime.InteropServices.ExternalException> instead. |
| 23 | + |
| 24 | +## Type of breaking change |
| 25 | + |
| 26 | +This is a [behavioral change](../../categories.md#behavioral-change). |
| 27 | + |
| 28 | +## Reason for change |
| 29 | + |
| 30 | +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. |
| 31 | + |
| 32 | +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. |
| 33 | + |
| 34 | +## Recommended action |
| 35 | + |
| 36 | +If your code catches `OutOfMemoryException` when using System.Drawing APIs, ensure you also catch `ExternalException` to handle these GDI+ errors. |
| 37 | + |
| 38 | +```csharp |
| 39 | +try |
| 40 | +{ |
| 41 | + // System.Drawing operations |
| 42 | +} |
| 43 | +catch (ExternalException ex) |
| 44 | +{ |
| 45 | + // Handle GDI+ errors (including former OutOfMemoryException cases) |
| 46 | +} |
| 47 | +catch (OutOfMemoryException ex) |
| 48 | +{ |
| 49 | + // Handle actual memory issues |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +## Affected APIs |
| 54 | + |
| 55 | +All System.Drawing APIs that interact with GDI+ and previously could throw `OutOfMemoryException` for `Status.OutOfMemory` errors, including but not limited to: |
| 56 | + |
| 57 | +- <xref:System.Drawing.Bitmap?displayProperty=fullName> constructors and methods |
| 58 | +- <xref:System.Drawing.Graphics?displayProperty=fullName> methods |
| 59 | +- <xref:System.Drawing.Image?displayProperty=fullName> methods |
| 60 | +- <xref:System.Drawing.Icon?displayProperty=fullName> constructors and methods |
| 61 | +- Other System.Drawing types that use GDI+ internally |
0 commit comments