Solved

Best way to detect file is image?

Posted on 2010-11-24
6
592 Views
Last Modified: 2012-05-10
Hello,

My web form allows users to upload files. If the file is an image, I like to show a thumbnail of the image.

The form allows word docs and pdfs to be uploaded too. For these, I dont display an image.

So, what is the best way to detect a file is an image and to display it in an <img /> control?

Currently I base it on extension. if (ext == ".gif" || ext == "png || img == jpg) img.ImageUrl = file.Name

That list could go on, including tif, bmp. So is there a smarter way?

thanks
0
Comment
Question by:Valimai
6 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 34210023
Another way is to check data in an image file, i.e. file’s mime-type.

Have a look:

http://www.bitrepository.com/how-to-validate-an-image-upload.html
0
 
LVL 6

Expert Comment

by:AlfredRobot
ID: 34210037
you can use the following javascript code in this link:
http://forums.asp.net/p/1590719/4029169.aspx

However, if you want to make it more precisely(e.g., some body changed the extension from jpg to doc), it's better for you to check the image file header:
http://www.mikekunz.com/image_file_header.html
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 50 total points
ID: 34210044
Also have a look at one possible way to check MIME types (lot of others on Internet):

http://stackoverflow.com/questions/2826808/how-to-identify-the-extension-type-of-the-file-using-c

List of MIME types:

http://www.webmaster-toolkit.com/mime-types.shtml
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34210184
For what it's worth, I did a simple test page with a GIF, JPG, PNG, TIF, and BMP image.  Safari was the only browser that displayed the simple uncompressed TIF along with the rest.  IE6/8, Firefox, Chrome, and Opera displayed all the others.
0
 
LVL 8

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 200 total points
ID: 34210294
You can use GetFileInfo() WIN32 API

System.IO.FileInfo fInfo = new System.IO.FileInfo(openFileDialog1.FileName)

or
Search in registry to get the extention of file
or
Can use  Path.GetExtension(string path)

or follow this link
http://www.codeproject.com/KB/cs/GetFileTypeAndIcon.aspx


After getting the extension, you can know whether it's an image file or not.
hints:

string extension = Path.GetExtension(FileInput.PostedFile.FileName);

switch(extension.tolower())
{
case ".gif":

case ".jpg":

case ".bmp":
             //Print it is an image file
             //here add all possible ext.s

................
}


or keep all the possible image file exts. inside an array and iterate through it.
It'd be more powerful if you will check the binary whether it's a valid imagefile or not
0
 
LVL 1

Author Comment

by:Valimai
ID: 34214745
Thank you all for contributing!

I had decided to detect the mime type of all files and have have implemented the registry method for now. It helped me in another area. One down side is the PDF example where PDF will only be detected if Acrobat reader is installed for example. So when the application is on the server, PDF is not someone you tend to install on that. So I may implement the list solution.

As for actually detecting an image, mime type is no good because you get mimetypes of "image/[foobar" when they are actually video files. So I have decided to detect based on extension. This only presents a problem when you detech JPG, but not the heritage extensions of JPE JPEG. Im not too concerned.

thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question