Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ImageFormat from File Extension

Tags:

c#

.net

image

Is there quick way to get the ImageFormat object associated to a particular file extension? I'm looking for quicker than string comparisons for each format.

like image 986
Chris Avatar asked Aug 26 '09 21:08

Chris


People also ask

How do you determine if a file is a PNG?

Open a file in a Hex editor (or just a binary file viewer). PNG files start with 'PNG', . jpg files should have 'exif'or 'JFIF' somewhere in the beginning.

How can I tell what file type an image is?

If you are having trouble and want to check if you photo is a JPEG, look at the writing under the photo in its file name. If it ends . jpg or . jpeg- then the file is a JPEG and will upload.


1 Answers

Here's some old code I found that should do the trick:

 var inputSource = "mypic.png";  var imgInput = System.Drawing.Image.FromFile(inputSource);  var thisFormat = imgInput.RawFormat; 

This requires actually opening and testing the image--the file extension is ignored. Assuming you are opening the file anyway, this is much more robust than trusting a file extension.

If you aren't opening the files, there's nothing "quicker" (in a performance sense) than a string comparison--certainly not calling into the OS to get file extension mappings.

like image 54
richardtallent Avatar answered Sep 18 '22 22:09

richardtallent