tags:

views:

246

answers:

2

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.

A: 

see the CodeProject article on File Associations http://www.codeproject.com/KB/dotnet/System_File_Association.aspx

Robert French
+1  A: 

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

 string InputSource = "mypic.png";
 System.Drawing.Image imgInput = System.Drawing.Image.FromFile(InputSource);
 Graphics gInput = Graphics.fromimage(imgInput);
 Imaging.ImageFormat 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.

richardtallent