Groups | Blog | Home
all groups > dotnet drawing api > august 2003 >

dotnet drawing api : recognize image files



Oscar Papel
8/29/2003 12:40:03 PM
Most image file formats have an identifying byte sequence (sometimes called
a magic cookie) in the first few bytes of the file that identify it as a
potentially valid file.
For instance, all BMP's start with the characters "BM".
You could check for this but it still wouldn't solve your problem.
This would not handle the pathological case where a file just happened to
have the right sequence but was not a valid file. Also, image files can get
corrupted. Finally, GDI+ has incomplete implementations of some file
formats (e.g. TIFF) so perfectly valid image files are not able to load.
Your only option is to try and load it and handle the Exception that is
thrown (usually it is a competely unhelpful generic GDI+ error).
As for extensions, there is a list available in the
System.Drawing.Imaging.ImageFormat enumeration. This list probably won't
change, however.

Good luck

Oscar

[quoted text, click to view]

dotNET rookie
8/29/2003 5:04:44 PM
Hi,
I'd like to know if there's a way to identify a file as a valid bitmap which
can be loaded with the public Bitmap(string filename) constructor.

Should I test filename's extension or is there a way to check the header ?
Maybe there's a function which gives me supported extensions so I won't have
to change my code if a new format arrives in a next version of the
framework.

Or I can try...catch exceptions, but is there a defined exception of type
UnsupportedBitmapException ?

Please show me the way, thanks.

AddThis Social Bookmark Button