Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Best way to detect file is image?

Posted on 2010-11-24
6
Medium Priority
?
597 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 30

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 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 84

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 9

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 800 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

705 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