Preventing Image.FromFile exception in VB 2022 when the file does not have a valid image format

Ron Sipherd 276 Reputation points
2025-04-23T16:47:11.57+00:00
The Visual Basic 2022 Image.FromFile method throws an​ OutOfMemoryException if the file does not have a valid image format. 
Is there a way to determine in advance whether the candidate file​ has a "valid image format"?
At present I'm using a Try...Catch block, but would prefer an​ alternative. 
The help file says "This type of OutOfMemoryException exception represents a catastrophic failure. If you choose to handle the exception, you should include a catch block that calls the Environment.FailFast method to terminate your app"
That's pretty drastic, would like to forestall it.
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,827 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.