Click here to Skip to main content
Email Password   helpLost your password?

Introduction

Several image file formats enable you to store metadata along with an image. Metadata is information about an image, for example, title, width, camera model, and artist. Microsoft Windows GDI+ provides a uniform way of storing and retrieving metadata from image files in the Tagged Image File Format (TIFF), JPEG, Portable Network Graphics (PNG), and Exchangeable Image File (EXIF) formats. In GDI+, a piece of metadata is called a property item, and an individual property item is identified by a numerical constant called a property tag.

In .NET Framework, you can store and retrieve metadata by calling the SetPropertyItem and GetPropertyItem methods of the Image class, and you don't have to be concerned with the details of how a particular file format stores that metadata.

Another question is how can you use this data. You will receive just an array of bytes, and it is your problem how you interpret it. This library will simplify access to this image metadata by converting them to ordinary .NET data types and organizing them in the form of class properties and/or hashtable.

The primary source of information to write this class was MSDN.

MSDN Home > MSDN Library > Graphics and Multimedia > GDI+ > GDI+ Reference > Constants

I'd like also to thank Jeffrey S. Gangel whose article "Photo Properties" at CodeProject brought me to the idea to write this class. His program makes basically the same, the only difference is that I wanted to have image information in the form that seems to me to be more comfortable and conventional.

Not all image property tags are implemented as class properties. Those are accessible only from PropertyItems hashtable. If anyone needs this library at all and even needs the specific property, I'll be very glad to help him. But I think it is not necessary, because the whole source code is available and modifiable.

Usage

// Accessing image proprties

public void ShowEquipModel() 
{ 
    Info inf=new Info("c:\\tmp\\Example.jpeg"); 
    MessageBox.Show(inf.EquipModel);
}
// Accessing image proprties as a hastable 

public void ShowAllProperties() 
{ 
    Info inf=new Info("c:\\tmp\\Example.jpeg");
    listView.Items.Clear(); 
    foreach (string propertyname in inf.PropertyItems.Keys) {
        ListViewItem item1 = new ListViewItem(propertyname,0);
        item1.SubItems.Add((inf.PropertyItems[propertyname]).ToString());
        listView.Items.Add(item1); 
    } 
}

The properties of the Info class are listed below:

See also

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralThanks!
zamirinima
5:50 10 Feb '10  
it's working very well in C#!
Nima
GeneralCan I use this dll?
u8521001
2:10 30 Sep '08  
Can I build an application uses your EXIFextractor.dll ?
Of course, my application is not for commercial purpose, but I may release it for free and ask users don't use it to make money.
I hope your answer is positive.
Thanks

Jack Chi-Chang Hsu
Taipei,Taiwan
AnswerRe: Can I use this dll?
George Mamaladze
12:21 30 Sep '08  
You can copy, reuse or change it without any restrictions. It is absolutely free. Smile
GeneralProblem - Image files are left locked
johnieT
4:50 23 Sep '08  
Hi

Great stuff, but I've a bit of a problem.

I've written a classs to save uploaded images, using your library to get the metadata. Something like this:

Me.PostedFile.SaveAs(strOutPut)
Me.PostedFile = Nothing dim strMetaInfo as string
strMetaInfo = getProperties("/" & Me.RootFolder & Me.ScrapFolder & "/" & Me.strFileName)

..... // code to resize, rotate etc.

Function getProperties(ByVal strImage As String) As String Dim inf As New Info(HttpContext.Current.Server.MapPath(strImage))
Dim propertyName As String Dim strProps As String = "" For Each propertyName In inf.PropertyItems.Keys
strProps += propertyName & ": " & inf.PropertyItems(propertyName).ToString amp; "<br/>" Next getProperties = strProps
inf = Nothing End Function


The trouble is if I try to upload the file again, I get the error:
the process cannot access the file "x.jpg" because it is being used by another process
I've a feeling the Info class isn't dispose()ing of something correctly.

Thanks again

Johnie
GeneralRe: Problem - Image files are left locked
johnieT
2:15 24 Sep '08  
Hi again

I solved this one myself, by adding the following to Info.cs

 
public void getrid() {
_image.Dispose();
}

See here http://www.odetocode.com/Articles/417.aspx[^][^]

for simple notes on how to recompile on the command line.

My code now goes
Function getProperties(ByVal strImage As String) As String Dim inf As New Info(HttpContext.Current.Server.MapPath(strImage))
Dim propertyName As String Dim strProps As String = "" For Each propertyName In inf.PropertyItems.Keys
strProps += propertyName & ": " & inf.PropertyItems(propertyName).ToString amp; "<br/>" Next getProperties = strProps
inf.getrid() inf = Nothing End Function

Johnie
QuestionTerms of Use?
Omnicoder
15:19 22 Apr '08  
Can I use this in an application that may eventually be commercial?
AnswerRe: Terms of Use?
George Mamaladze
10:22 23 Apr '08  
This code is free for use, copy and duplicate modified or unmodified. The only requirement is a positive vote! Smile
Questionsome EXIF data not being read on some files?
todd_001
15:50 16 Jan '08  
Hi there

Firstly I want to thank you very much for this article. I was very happy when I found it. It seemed to fit the glove perfectly for what I was looking to do, and it works *almost* all the time. However.. something is happening that has me stumped. Maybe it's simple since I 'm definitely no guru.
All the pictures that I've been playing with so far were all taken with the same camera and have been subject to the same process to download them off of the camera.

I can access the EXIF data from most pictures, but there is a small group of pictures that it doesn't seem to read the data for.. I'd like to focus in on the "DtOrig" property since that is the one I'm most interested in, and the one that is particularly confusing. I thought originally that it was an error in my code (which uses your .dll) but then after tangling with it for awhile I tried your demo on the files and the demo also cannot read the DTOrig data on the particular files in question.

Fine then you say, the data just isn't there. But I'm using Vista, and when I right-click on the picture, and go to the Details tab, the data is correctly displayed there - so I'm thinking it is definitely encoded in the file.

I'm hoping that you can help me figure out how to get the data out of these files.
If it is helpful, I'll post one of these mysterious pictures, and you can try it out yourself.

Thanks for any help.
GeneralRe: some EXIF data not being read on some files?
George Mamaladze
23:56 16 Jan '08  
Hi,
send me a picture having the described problem. I'll try to find out why it does heppens.
GeneralRe: some EXIF data not being read on some files?
todd_001
9:58 17 Jan '08  
Hi George

I've posted the pictures here and named them appropriately:
http://cid-a941f66695c4da22.skydrive.live.com/browse.aspx/test/exif_data_problem[^]

ie:
picture1_DataFound.jpg <-- DTOrig is viewable in both windows and the demo program
picture2_DataNotFound.jpg <-- DTOrig is viewable in windows but not in the demo program

The pictures are of our new little baby boy Smile

Thanks again for taking a look.

Todd
GeneralRe: some EXIF data not being read on some files?
George Mamaladze
11:06 17 Jan '08  
Sorry, it seems I am not able to help you. The problem occurs when reading apropriate image properties from raw file using .NET native metod.

System.Drawing.Image.GetPropertyItem

Either there is a bug in you camera Smile or in framework itself. I hate conclusions like this but it seems to be right in this case.

I wish you and your new born young man everithing all the best! I would not say he is littleSmile
GeneralRe: some EXIF data not being read on some files?
todd_001
16:57 19 Jan '08  
Hi George

Thanks for looking into it.

I found a post on an msdn blog that *might* provide a clue as to why Vista knows the original date taken, and it is not readable in the exif data.

I noticed that the EXIF 'Software' property said "Microsoft Windows Photo Gallery 6.0.6000.16386" That is the software used to download the pictures from the camera, and it may also be an ominous signature leading back to the cause of my problem.

That value indicates to me that the camera is not the only one writing to the EXIF data, and if photo gallery is writing to the EXIF data, maybe it has some hand in messing up the DTOrig value. Especially since, the vista file 'Details' tab is presumably reading from the XMP data, and IT contains the correct date taken.

The only place I figure photo gallery can get the DTOrig value in the first place is from the EXIF data, since the camera clearly wouldn't know how to write to both EXIF and XMP.

So.. following this theory, Windows Photo Gallery is using the DTOrig and copying it into the XMP formatted metadata and in doing so, somehow corrupting the DTOrig EXIF data - making it unreadable by yours or any other EXIF reader I've tried.

SO... all that being said, I'm going to try and experiment some more to explore my idea, and find a way to read that XMP meta data from the file.

Here's the blog post I referrerd to. If you have any insights, ideas, or advice, it's most certainly welcome.
http://blogs.msdn.com/pix/archive/2006/08/16/702780.aspx[^]

-Todd
GeneralRe: some EXIF data not being read on some files?
todd_001
19:21 19 Jan '08  
I managed to extract the XMP data from the file.
If you open the jpg in notepad, you can see it right there in plain text.
I wrote the code to get the DTOrig value based off of this guys example.

[^]


So in my code, I check the exif property, and if it isn't available then I read the xmp data, and look for the data there.

I'm not sure why MS would do a destructive read on the DTOrig exif property value. Maybe it's some kind of bug, but that wouldn't be very likely since Microsoft stuff never has bugs. D'Oh!
GeneralGreat work
Davorin Sajnovic
0:55 22 Apr '07  

This is very useful. I'm needed to get info from photo and this is just what I needSmile .
Excellent job!
Thank you.

Regards,
Davorin!
GeneralI used it, get the source
motisaadon
0:27 20 Mar '07  
Hi,
thanks for the code,
I wrote an aplication that use this dll

http:\\www.upload.co.il\eng\creation.asp?id=424

GeneralResolutionUnit
damoose75
3:26 21 Dec '06  
Thanks...this is great and saved me a ton of effort! I did run into an issue though - once I accessed the metadata I tried to modify the tiff ResolutionUnit. The default seems to be 2 and I wanted to change it to a 1. It appears to allow and retain the change during runtime, but once I save the image it appears to default back to 2. I can't quite tell what I am doing wrong...any help I can get would be much appreciated.

prop.Id = 296;
prop.Value[0] = 1;
tiffImage.SetPropertyItem(prop);
tiffImage.Save(@"D:\Work\Trial\ImageConversion\Bitonal\Images\8.tif");


rm
GeneralSpeed ups [modified]
lummie
8:53 25 Oct '06  
Firstly thanks, oh thanks, oh thanks for a superb class. This has saved my a lot of time.

I came across a couple of optimizations that I thought you might be interested in.

Firstly I took out the _image reference and added a

private PropertyItem[] _metadata;

so the whole image was not in memory for the scope of info instance.

In addition, I found this article
http://www.pixvillage.com/blogs/devblog/archive/2005/03/17/159.aspx

which provides a quicker method of loading the image data. So I added this function, which your constructor calls

private void FastLoadImage(string imageFileName)
{
using (FileStream stream = new FileStream(imageFileName, FileMode.Open, FileAccess.Read))
using (Image image = Image.FromStream(stream,true,false))
_metadata = image.PropertyItems;
}

I then created a couple of wrapper methods for getting the info from the _metaData variable

private PropertyItem GetMetaData(PropertyTagId tag)
{
PropertyItem item;
for (int i = 0; i < _metadata.Length; i++)
{
item = _metadata[i];
if (item.Id == (int)tag)
return item;
}
return null;
}

private Object GetMetaDataValue(PropertyTagId tag)
{
return PropertyTag.getValue(GetMetaData(tag));
}

which simplifies property call
e.g.
return (Fraction) GetMetaDataValue(PropertyTagId.XResolution);


I managed to extract the metadata from 6288 files in 42 seconds after these changes.

Though you might be interested.




-- modified at 13:59 Wednesday 25th October, 2006
GeneralExtract image (tiff and jpg) metadata
mindang
21:03 15 Oct '06  
Hi,
I've downloaded the EXIFextractor.dll but I don't know how to refer to it. Would you please show me how? (I'm asp.Net programmer)
Thanks
GeneralRe: Extract image (tiff and jpg) metadata
George Mamaladze
22:18 15 Oct '06  
Hi,
first of all add a reference to your ASP.NET project. Then add using gma.Drawing.ImageInfo; at top of your codebehind class. Now you are ready to use it in your page. Add this snippet for example in Page_Load method

Image img = Image.FromFile("c:\test.jpg"); 
Info inf=new Info(pictureBox.Image);
foreach (string propertyname in inf.PropertyItems.Keys)
{
Response.Write(propertyname,
(inf.PropertyItems[propertyname]).ToString());
}

GeneralRe: Extract image (tiff and jpg) metadata
mindang
17:02 16 Oct '06  
Thank you very much for your help.
Now on top of code behind page when I add Imports I can see the gma.Drawing.ImageInfo there. But your example to test the dll I think is not asp.net. Can convert those lines to asp.net syntax (I'm just a beginer)?

Thanks,
Regards

MD
GeneralRe: Extract image (tiff and jpg) metadata
mindang
17:43 16 Oct '06  
Thank you,
I've found a gma.Drawing.ImageInfo help. It works now.

Thanks.
MD


Generalowner url
wijesijp
6:47 6 Sep '06  
I have used your program to get metadata of images.
When you open the image in Photoshop you will see an owner url field.

I couldn’t find that value from the application.
How do you access that field?

Janaka

QuestionHow would i write this in Visual Basic?
mac.macananny
5:32 18 Aug '06  
Beings i don't know C that well, is there a way to port this to visual basic? I am trying to massive update all of the datecreated tags with the OriginalDate tag with about 7000 pictures.

Mac

AnswerRe: How would i write this in Visual Basic?
George Mamaladze
6:52 23 Aug '06  
I can advice you to leave the code on c# compile it as an assembly and refer to it from vb.net.
AnswerRe: How would i write this in Visual Basic?
AlenCroatia
2:09 24 Nov '08  
Hi,
if You successfully import code in Visual Basic can you send me how it look in VB.

Alen.


Last Updated 4 Aug 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010