Saving new EXIF metadata in .NET

written by $post.User.ProperName on Thursday, January 17 2008

 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.

 

Similar Posts

  1. Improving Your Internet Sales Performance
  2. An Easy .NET CF Settings Class
  3. Never Buy Creative Labs Products

Comments

  • Magnus on on 12.05.2008 at 5:22 PM

    Magnus avatar

    Brilliant, thanks!

    // Magnus;

Post a comment

Options:

Size

Colors