Image.FromFile and exceptions
-
In the .NET Framework, what exception would you expect Image.FromFile to throw if the image is invalid? Something like an "InvalidImageFormatException" or similar? Nope. It throws an OutOfMemoryException. MSDN says:
OutOfMemoryException
The file does not have a valid image format.
-or-
GDI+ does not support the pixel format of the file.Really? I doubt the system is actually running out of memory as it returns quickly.
-
In the .NET Framework, what exception would you expect Image.FromFile to throw if the image is invalid? Something like an "InvalidImageFormatException" or similar? Nope. It throws an OutOfMemoryException. MSDN says:
OutOfMemoryException
The file does not have a valid image format.
-or-
GDI+ does not support the pixel format of the file.Really? I doubt the system is actually running out of memory as it returns quickly.
It does what the documentation says it does. That is positive in general, and it sounds a bit weird in this case. Anyhow, it is a lot better than getting a "General error occurred in GDI+" which can mean almost anything. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
In the .NET Framework, what exception would you expect Image.FromFile to throw if the image is invalid? Something like an "InvalidImageFormatException" or similar? Nope. It throws an OutOfMemoryException. MSDN says:
OutOfMemoryException
The file does not have a valid image format.
-or-
GDI+ does not support the pixel format of the file.Really? I doubt the system is actually running out of memory as it returns quickly.
You are probably trying to read an image that is not valid. If you notice, there is no such exception like "Invalid Image" listed out.
OutOfMemoryException
The file does not have a valid image format.
-or-
GDI+ does not support the pixel format of the file.FileNotFoundException
The specified file does not exist.ArgumentException
filename is a Uri. -
You are probably trying to read an image that is not valid. If you notice, there is no such exception like "Invalid Image" listed out.
OutOfMemoryException
The file does not have a valid image format.
-or-
GDI+ does not support the pixel format of the file.FileNotFoundException
The specified file does not exist.ArgumentException
filename is a Uri.Yeah, I'm validating that an uploaded image is actually a valid image. I was just ranting about the framework using an OutOfMemoryException to signal an exception that doesn't relate to memory at all :)
-
Yeah, I'm validating that an uploaded image is actually a valid image. I was just ranting about the framework using an OutOfMemoryException to signal an exception that doesn't relate to memory at all :)
Daniel Lo Nigro wrote:
I was just ranting about the framework using an OutOfMemoryException to signal an exception that doesn't relate to memory at all
Not necessarily true. It could be a bug in GDI+ that recursively tries to load the bad image and results in a real Out of Memory situation. :-)