Tutorial :Is a file an image?


In C# what is the best way to tell if a particular file is an image?


Most picture formats specify the file type in the first few bytes of the image. You can read in a few bytes and look for the correct headers.

File extensions technically don't hold any important data about the image. It just helps the OS figure out what program to use to open it. (But, checking the extn is probably the easiest way, and usually correct.)


This isn't tested, but it's something like this:

private string MimeType (string Filename)  {      string mime = "[default]";      string ext = GetExtension(Filename).ToLower();      Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);      if (rk != null && rk.GetValue("Content Type") != null)          mime = rk.GetValue("Content Type").ToString();      return mime;  }  

(Sorry, it's been awhile since I've done registry stuff)


The way we did it (and i cant find the exact code right now so the following is untested) is to try to load the image as a bitmap. If it fails, its not an image

bool isImage;  try  {    Bitmap.FromFile(filePath);    isImage = true;  }  catch  {    isImage = false;  }  


I don't think there's any way of guaranteeing that a given file (or any set of bytes) necessarily represents an image other than trying to load and display it.

However, if you have some mystery bytes, you could always check for certain byte signatures to try to deduce if it is a well formed image file. You can look up file headers on wikipedia, but here's some code I use in unit testing to verify the output of a third party reporting tool:

// Some signatures:    const string _pdfSignature = "%PDF-";  readonly byte[] _bmpSignature = new byte[2] { 0x42, 0x4D };  readonly byte[] _pngSignature = new byte[8] {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};  readonly byte[] _gifSignature = new byte[6] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 };  

And for JPGs:

Assert.That(_theBytes[0] == 0xFF && _theBytes[1] == 0xD8);    //Start of jpg file  Assert.That(_theBytes[_theBytes.Length - 2] == 0xFF && _theBytes[_theBytes.Length - 1] == 0xD9); //End of jpg file  


Most image files have a magic byte or two at the very beginning of the file. This is used by decoders and image viewers to identify the file type. There is no one rule that can help you.


If you need to validate that the file is a valid image format then you are going to have to have some sort of code try and parse the contents according to the type of the file you are expecting. To do that, you would call the static FromFile method on the Image class to try and get an Image instance.

You could check the first few bytes to see if the marker for a particular image format is there but just like checking the file extension, it doesn't guarantee that the contents are a valid image format.

If you don't need validation though, checking the file extension or mime type is just fine.


check the file extension


Admittedly I haven't done too much with this but isn't this simply looking up the mime-type?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »