Saving new EXIF metadata in .NET

by JasonRShaver 17. January 2008 16:40

 Ok, it turns you that you cannot create new EXIF tags in .NET, they seem to expect you to steal a existing EXIF tag, change it, and save that.  Well what if your image does not have ANY EXIF tags?  Well here it is:

PropertyItem ThisProp = (PropertyItem)Activator.CreateInstance(typeof(PropertyItem),true);

ThisProp.Id = 270;

ThisProp.Type = 2;

ThisProp.Len = Encoding.ASCII.GetByteCount(bioID);

ThisProp.Value = Encoding.ASCII.GetBytes(bioID);

image.SetPropertyItem(ThisProp);

 

try

{

FileInfo SaveFile = newFileInfo(String.Format(@"{0}\{1}-{2}.jpg",

              txtSaveDirectory.Text, txtFilenamePrefex.Text,

              DateTime.Now.ToString("yyyyMMddhhmmss")));

image.Save(SaveFile.FullName, ImageFormat.Jpeg);

}

catch (Exception ex)

{

MessageBox.Show(

String.Format("Error ({0}) saving image.\n\n{1}"

, ex.GetType().ToString(), ex.Message)

                     "Error Saveing Image");

}

This method basically uses reflection to create a new PropertyItem using its private constructor.  I will disclaim that this is a bad idea, but in my cause it is only being used for an internal tool that only needs to last a few weeks. 

If anyone finds a better way that does not require me to embed images, steal other images tags, or hand process JPEG files, then I am all for it.

 

Tags:

Blog

About the author

I am a software developer working for Microsoft in Redmond, WA.  In addition, my wife and I own TTXOnline, what is likely the 3rd largest table tennis store in the US.

Month List

Page List