Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if a file is an image file in .NET?

I don't want to rely on the file extension. I don't care to know what type of image it is (.jpg, .png, etc.), I simply want to know if the file is an image or not. I'd prefer to not use any non-.NET dlls if possible.

The best way I know how to do this is the following:

bool isImageFile;
try
{
    Image.FromFile(imageFile).Dispose();
    isImageFile = true;
}
catch (OutOfMemoryException)
{
    isImageFile = false;
}

As noted here: http://msdn.microsoft.com/en-us/library/stf701f5.aspx, Image.FromFile() throws an OutOfMemoryException if the file isn't a valid image format. Using the above gives me exactly the result I want, however I'd prefer not to use it for the following reasons:

  • It is my belief that using try-catches for normal program execution is a bad practice for performance reasons.
  • Image.FromFile() loads the whole image file (if it is an image file) into memory. This is wasteful I assume because I only need the file type and don't need to do any further image manipulation at this point in my code.
  • I dislike catching OutOfMemoryExceptions because what if there is a REAL out-of-memory problem and my program swallows it and keeps going?

Are there any better ways to do this? Or, are any/all of my concerns listed above unwarranted?

Edit: Since receiving the answers here, these are the three solutions I'm now aware of:

  1. Load the whole image in memory via Image.FromFile() and a try-catch.
    • Pros: Does a deeper check against the image files contents; covers many image types.
    • Cons: Slowest; overhead from try-catch and loading full image file into memory; potential danger from catching a 'real' OutOfMemoryException.
  2. Check the header bytes of the image file.
    • Pros: Quick, low memory usage.
    • Cons: potentially brittle; need to program for every file type.
  3. Check the file extension.
    • Pros: Quickest; simplest.
    • Cons: Doesn't work in all situations; most easily wrong.

(I do not see a clear "winner" since I can imagine a situation in which each one would be appropriate. For my application's purposes, the file type checking happens infrequently enough that the performance concerns of method 1 weren't an issue.)

like image 317
Daryl Avatar asked Feb 20 '12 00:02

Daryl


People also ask

How can I tell what file type an image is?

Search Google Images by file type Luckily, there's a way within Google to specify that: just type filetype: + the format you need right into the search field with your query. For example, t-rex filetype:jpg.

How do I validate a file type in C#?

bool CheckFileType(string fileName) { string ext = Path. GetExtension(fileName); switch (ext. ToLower()) { case ". gif": return true; case ".

How do you check if a file is an image in PHP?

Using file extension and getimagesize function to detect if uploaded file has right format is just the entry level check and it can simply bypass by uploading a file with true extension and some byte of an image header but wrong content.


1 Answers

If you will only be supporting a handful of the popular image formats, then you can simply read the first few bytes of the file to determine the type based on the Magic Number

Examples from the link provided:

  • GIF image files have the ASCII code for "GIF89a" (47 49 46 38 39 61) or "GIF87a" (47 49 46 38 37 61)
  • JPEG image files begin with FF D8 and end with FF D9. JPEG/JFIF files contain the ASCII code for "JFIF" (4A 46 49 46) as a null terminated string.
  • PNG image files begin with an 8-byte signature which identifies the file as a PNG file and allows detection of common file transfer problems: \211 P N G \r \n \032 \n (89 50 4E 47 0D 0A 1A 0A).
like image 136
tawman Avatar answered Oct 11 '22 16:10

tawman