With all the smarts of actually loading images being done by the .net framework, seems like I shouldn’t have to repeat it all in my code by checking for magic numbers, or using a hack like this:
Private Function IsImage(FileName as String) As Boolean
Try
Using img As New Bitmap(FileName)
End Using
Catch ex as System.ArgumentException
Return False
End Try
Return True
End Function
Am I missing something obvious, like System.Drawing.IsImage(stream)?
You will need to open up the file and read the relevant headers for the file types you want to support, as mentioned here:
determine if file is an image
I don’t think there is anything already in the .NET framework that can do this for you, other than loading it into an image and querying the image format:
Find image format using Bitmap object in C#
An alternative theory (no actual facts to back this one up): perhaps the file in Windows holds meta-data that flags it as an image – in the same manner than the properties dialog seems to show artist information for audio files. This could be a cute way to avoid opening the file.
Edit by FastAl Jun 2020. More useful links:
Using .NET, how can you find the mime type of a file based on the file signature not the extension
Not what I asked for, but here are the magic #s:
https://en.wikipedia.org/wiki/List_of_file_signatures
https://www.garykessler.net/library/file_sigs.html