473,671 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7598
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.comw rote in message
news:uZ******** ********@TK2MSF TNGP02.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.comw rote:
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 "PropertyIt ems" 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.comw rote:
>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 "PropertyIt ems" 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.comw rote:
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
PropertyTagJPEG InterFormat and PropertyTagJPEG InterLength 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\x1 A\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\x1 A\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
3343
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 src=getimage.php?tbl=dbo.Employees&type=jpg&col=Photo&idCol=EmployeeID&id=1 Border=0 alt=''>&nbsp;</td>
3
11755
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 table in MySQL with the path & filename to the image. I have successfully uploaded and performed an update query on the database, but the problem I have is I cannot retain the primary key field in a variable which is then used in a SQL update...
0
265
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 whether there's a standard format for such strings as there is for types: <config type="MyNamespace.MyType, MyAssembly"/> I guess I need something like:
3
2832
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 one circle for each row. I was wondering if I could use the Python Imaging Library to process these forms. I know the Image class is really powerful, but I can't think of a way of how to locate wich circle was filled.
10
4498
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 consideration here is that it is supplied to the AsyncOperationManager.CreateOperation(userSuppliedState) method... with userSuppliedState being, more or less, a taskId. In this case, the userSuppliedState {really taskId} is of the object type,...
7
2447
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 happening because the Images are not cached (yet) on the users browser. I tried preloading images with the 2nd code below in the childpage head and in the parent page head and both. no joy, dosent seem to actually 'preload' the pics. (i also gave...
5
6143
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, ReadOnly, etc.). The problem I'm having is in identifying when a FileSystemInfo entry is a FileInfo or a DirectoryInfo type. I get the rare, and yet oddly common, "setup.inf" file that for some inexplicable reason passes the standard...
3
2326
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 programmer, I have no control over this
0
10753
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 inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
8402
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8927
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8825
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8676
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5703
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.