Re: Identifying an image type.



On Tue, 22 May 2007 11:02:28 -0700, Frank Rizzo <none@xxxxxxxx> wrote:

I have an interesting problem. I have a directory of image files. However, none of the files have an extension. I need to figure out what type if image it is and attach an extension to the file. Is there a way to determine image type in the .net framework?

As Ignacio says, you may not really need to have the correct extension, if the use of the files is limited to your own application. You can just try to open each file using Image.FromFile() and any file that works is a valid image file for .NET purposes. :)

That said, you might want to fix up the extensions so that the files make sense externally to some other software or for some other reason. For that, I don't know of any general-purpose, reliable method. Each image file format has its own header and data format, and other than inspecting that data directly, you can't determine the file format.

If there are specific file formats that you want to be able to handle, it should be simple enough to research each format and figure out what the header looks like. For every image file format I know about, the initial part of the header includes some unique sequence of bytes. To handle the most basic cases, it should not require much effort, though it will be tedious since you'll have to create some sort of table that includes the unique sequence of bytes, where that sequence is found in the file, and a file extension to associate with that sequence.

For what it's worth, once you've opened a file in .NET with the Image.FromFile() method, you can look at the "PropertyItems" property for the image to glean some limited information about the file. Unfortunately, the properties are mostly general-purpose and not specific to any one file format. However, there are a couple of JPEG-specific properties that, if they exist, should indicate that the image was read from a JPEG file. That doesn't really solve the more general case though, and is probably not worth pursuing unless what you really want is simply a way to distinguish JPEG files from other files.

Pete
.



Relevant Pages

  • Re: Save Picture As.. is saving every jpg with the extension .jpe
    ... In the rght side you will see the 'Extension' value is likely to be .jpe (if ... MS MVP/Windows - Internet Explorer ... Selecting 'Save Picture As' Does Not Save Image with Correct ... > saved as and unknown file format. ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • saving problems in OpenOffice 2.0
    ... Saving the file in the default file format didn't seem to work. ... When saving the files in OOO 2.0 there is a *"file type"* drop-down field containing the list of possible file formats this particular document can be saved as. ... Some items in this list are displayed as a combination of a file type description followed by the file extension in parentheses. ...
    (comp.unix.bsd.freebsd.misc)
  • Re: Check folder for duplicate files
    ... little ambiguous now that I've looked at it again), if you discover a file ... If they match (without the extension) then I have to both open them and check if they have the same data. ... Are you asking to examine the contents of a .pdf file as compared to a ..doc as compared to a plain text .txt file and parse them to see if the same words exist in the same order despite all the formatting someone apparently worked hard to accomplish and trash it for the original? ... you've bit off a big job as Mike says as there are an infinite number of possible ways and you'll have to do a complete lexical parsing of the file format to remove the superfluous information and uncover the fundamental "sameness" underlying it. ...
    (microsoft.public.vb.general.discussion)
  • Re: Save Picture As.. is saving every jpg with the extension .jpe
    ... Double click on the word 'Extension' and change it to .jpg ... > How to make a good newsgroup post: ... >> saved as and unknown file format. ... >> associations REG fix (found online after reading another post here). ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Re: Quest for HEX Strings
    ... # I assume that the mail header is still complete incl. ... # This script rebuilds a complete mail with extension .eml which may # be imported into a mail client and also saves the pure word file # with the extension .doc. ... my ($mailheader, $mailcontent) ... # Use this as a shortcut to save the word file directly without ...
    (de.comp.lang.perl.misc)