Solved

Best way to detect file is image?

Posted on 2010-11-24
6
590 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 82

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now