Search code examples
c#exif

Exif field ImageDescription adding data is not fully successful


Hope to seek some help. I am trying to add some text to Exif field ImageDescription(270). I am partially successful. Here is the code

pitem.Id = 270;
  pitem.Type = 2;
      byte[] utf16Bytes = Encoding.Unicode.GetBytes("Testing ImageDescription from command line.");
      byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, utf16Bytes);
  pitem.Value = utf8Bytes;//utf16Bytes 
  image.SetPropertyItem(pitem);

I then save image copy at new path, then I try to read back the property just added. THIS is where I am so failing..:-( For some reason this field is not taking more then 6 characters, I have tried many things including change data to UTF-8 byte array, adding null-terminator (\0), even tried another field (305) with the same issue there as well, field will not take more then 6 characters.. I am not being able to see the full text being added to the field. Can some one guide..

Thanks


Solution

  • 2 things:

    (a) Look at PropertyItem.Type definition:

    1: Specifies that Value is an array of bytes.

    2: Specifies that Value is a null-terminated ASCII string. If you set the type data member to ASCII type, you should set the Len property to the length of the string including the null terminator. For example, the string "Hello" would have a length of 6.

    Looks like you are using it wrong - either it should be (1) - and then you should save array of bytes, or it should be (2) - but then you need to pass ASCII string to it.

    Look at this question as well: Set image meta data before save

    (b) Set ptype.Len property to the length of the string.

    http://msdn.microsoft.com/en-us/library/system.drawing.imaging.propertyitem.type.aspx