473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Identifying an image type.

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.
May 22 '07 #1
6 7562
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

"Frank Rizzo" <no**@none.comwrote in message
news:uZ****************@TK2MSFTNGP02.phx.gbl...
>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.

May 22 '07 #2
On Tue, 22 May 2007 11:02:28 -0700, Frank Rizzo <no**@none.comwrote:
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
May 22 '07 #3
Peter Duniho wrote:
On Tue, 22 May 2007 11:02:28 -0700, Frank Rizzo <no**@none.comwrote:
>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.
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.
May 22 '07 #4
On Tue, 22 May 2007 16:33:30 -0700, Frank Rizzo <no**@none.comwrote:
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
May 23 '07 #5
Frank Rizzo 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?
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
May 23 '07 #6
I figured it out thanks to wikipedia. Thank you.

Arne Vajhøj wrote:
Frank Rizzo 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?

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

May 24 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Pierre Tremblay | last post by:
Hi! I am trying to display an image in my html document. The document contains the following line: <td class="Input"><img...
3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
0
by: Kent Boogaart | last post by:
Hi, I'm in a situation where I need the user to specify a resource in via XML configuration. For example, they need to be able to specify an image embedded in their assembly. I'm just wondering...
3
by: Douglas Douglas | last post by:
Hi everybody. I have a paper form that I scan into an image. My user fills some circles in this paper form using black ink. Every form has ten rows with five circles each and the user fills only...
10
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
7
by: Inny | last post by:
Hello again, Im using the code below in a child page (popup), the images are called from the parent page. When the changer is running, the child page goes white between images. I realise this is...
5
by: Tom P. | last post by:
I am having the following problem: I create a FileSystemWatcher and wait for events. When the event does happen I refresh a FileSystemInfo list and set properties accordingly (IsFile, IsDir,...
3
by: Michael Powe | last post by:
Hello, Here is my problem: I have a site that uses urls of this format to submit forms: <a id="1_1_2_0" href="Javascript:if(validate)submitForm(...)">Submit</a> I'm just a third-party...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.