Re: Identifying an image type.
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Tue, 22 May 2007 12:56:12 -0700
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
.
- Follow-Ups:
- Re: Identifying an image type.
- From: Frank Rizzo
- Re: Identifying an image type.
- References:
- Identifying an image type.
- From: Frank Rizzo
- Identifying an image type.
- Prev by Date: CAPICOM throwing FileLoadException
- Next by Date: Re: "inherit" from a static class
- Previous by thread: Re: Identifying an image type.
- Next by thread: Re: Identifying an image type.
- Index(es):
Relevant Pages
|