views:

207

answers:

1

Some jpeg image is stored with .bmp ext. I want to use gdi+ to get the real type of the image file, how can I do this?

Many thanks!

+2  A: 

Here is a C# version (using the System.Drawing.Imaging namespace):

 public static ImageFormat getImageFileFormat( string filename )
 {
  using ( var fs = new System.IO.FileStream( filename, System.IO.FileMode.Open ) )
  using ( var img = Image.FromStream( fs, true, false ) )
   return img.RawFormat;
 }

With this function, you can do something like:

 ImageFormat fmt = getImageFileFormat( @"some file" );
 if ( fmt.Guid == ImageFormat.Jpeg.Guid )
 {
   ...
 }
 else if ( fmt.Guid == ImageFormat.Bmp.Guid )
 {
   ...
 }
danbystrom
Thank you very much, now I know how to do it in c++.