Open corranrogue9 opened 5 years ago
@corranrogue9 is this the same in .NET Framework?
I'm wondering whether we should just document this. (would move to https://github.com/dotnet/dotnet-api-docs)
Yes, the same in .NET framework as well. I think there are two parts here: the documentation reflecting reality, and that some ExternalException cases make it difficult to differentiate whether the callee or the caller has caused the error.
In addition, if the image is invalid, under Linux (Debian), it throws System.Runtime.InteropServices.ExternalException
("A generic error occurred in GDI+"). You can consider this a documentation issue, or a bug. Exact same code returns documented System.ArgumentException
under Windows.
It's pretty hard to make substantial improvements here. We are somewhat tied by generally not being able to change exception types and GDI+ often does not give great error info. There is also a bit of "magic" in interop error translation. We can incrementally add things to docs as we discover them, but it would be pretty hard to know that we've documented all cases.
The documentation for
Image.FromStream
indicates that an ArgumentException can be thrown if the provided image is not of a supported format. However, on Windows, it can also throw InvalidOperationException, ExternalException, FileNotFoundException, etc. as per the StatusException method. The callers of this method do not catch and wrap these exceptions into ArgumentExceptions.It should also be noted that an ExternalException can be thrown in the event that the stream does not contain a valid image. Further, in this case, the actual HResult is clobbered by E_FAIL, so the caller of Image.FromStream cannot even handle the case themselves by catching the ExternalException and checking for the appropriate HResult.
You can reproduce the above issue with the image generated by: