Identifying an image type.

  • Thread starter Thread starter Frank Rizzo
  • Start date Start date
F

Frank Rizzo

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?

Thanks.
 
Hi,

Not that I know of (and maybe you don;t even care) Image can use any file to
load an image, you can save it in any format you want

In any case take a look at the Image class in .NET
 
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
 
Peter said:
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.

That's exactly right - the files need to be processed by another system.
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.

No, there didn't seem to be any JPG specific entries.
 
No, there didn't seem to be any JPG specific entries.

Look again.
http://msdn2.microsoft.com/en-us/library/ms534416.aspx

In particular, at least for the JPEGs I looked at, the
PropertyTagJPEGInterFormat and PropertyTagJPEGInterLength were always
present, which makes sense to me.

There are lots of other JPEG-specific properties, but not all are ones I'd
expect to always be in a JPEG file.

In any case, looking at the properties only addresses a very narrow case.
It's not hard to parse the file headers, and if you want to handle a
variety of image file types, IMHO that would be the better way to go.

Pete
 
Frank said:
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?

There are no universal way but many files can be recognized
from the first bytes due to the existence of a header.

JPEG start with "\xFF\xD8"

GIF start with "GIF"

PNG start with "\x89PNG\r\n\x1A\n"

BMP start with "BM"

etc..

Arne
 
I figured it out thanks to wikipedia. Thank you.
There are no universal way but many files can be recognized
from the first bytes due to the existence of a header.

JPEG start with "\xFF\xD8"

GIF start with "GIF"

PNG start with "\x89PNG\r\n\x1A\n"

BMP start with "BM"

etc..

Arne
 
Back
Top