Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Introduce InvalidImageFormatException in .NET #13430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Wazi2025 opened this issue May 8, 2025 · 5 comments
Open

Introduce InvalidImageFormatException in .NET #13430

Wazi2025 opened this issue May 8, 2025 · 5 comments
Labels
area-System.Drawing System.Drawing issues untriaged The team needs to look at this issue in the next triage

Comments

@Wazi2025
Copy link

Wazi2025 commented May 8, 2025

Description

When attempting to load a non-image file (e.g., a PDF) using Image.FromFile(), .NET throws an OutOfMemoryException. This is misleading, as the error isn't due to memory exhaustion but rather an invalid file format.

Proposed Solution:

Introduce a new exception type, InvalidImageFormatException, to be thrown when Image.FromFile() encounters a file that isn't a valid image. This would provide clearer, more accurate error handling for developers.

Benefits:

Improved Clarity: Developers can more easily identify and handle errors related to invalid image formats.

Enhanced Debugging: Reduces confusion during debugging by providing a specific exception for this scenario.

Better Practices: Encourages more precise exception handling and promotes best practices in error management.

Reproduction Steps

using var image = Image.FromFile(filePath);

If the file in question is not an image file it throws an : 'System.OutOfMemoryException' in System.Private.Windows.Core.dll

Expected behavior

It should throw a new type of Exception instead of OutOfMemoryException.

Actual behavior

Exception thrown: 'System.OutOfMemoryException' in System.Private.Windows.Core.dll

Regression?

Not sure. I only started with C# in February this year.

Known Workarounds

No response

Configuration

  • .NET 9.0.203
  • Windows 10 Home (22H2)
  • x64
  • Don't think so
  • No Blazor

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged The team needs to look at this issue in the next triage label May 8, 2025
@huoyaoyuan huoyaoyuan added area-System.Drawing System.Drawing issues and removed needs-area-label labels May 8, 2025
@huoyaoyuan
Copy link
Member

Image is a thin wrapper of Windows GDI+. The OOM was returned from GDI+, not .NET.

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-drawing
See info in area-owners.md if you want to be subscribed.

@Wazi2025
Copy link
Author

Wazi2025 commented May 8, 2025

Fair enough. I'm just a C# rookie. I still think something should be done, though. The Exception is not correct and could lead to confusion (as it did in my case).

@JeremyKuhne JeremyKuhne transferred this issue from dotnet/runtime May 8, 2025
@JeremyKuhne
Copy link
Member

@Wazi2025 unfortunately GDI+ tells us that it is out of memory, and as such we don't have any context. That said, I plan to change the exception to something else as it is almost never a low memory situation. #12627 covers this, feel free to add additional feedback there.

@Wazi2025
Copy link
Author

Wazi2025 commented May 8, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Drawing System.Drawing issues untriaged The team needs to look at this issue in the next triage
Projects
None yet
Development

No branches or pull requests

3 participants