Solved

Best way to detect file is image?

Posted on 2010-11-24
6
594 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 9

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

710 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