articles:

Modifying Exif Information Using ASP.NET

There are two main problems you'll encounter when trying to modify exif information using asp.net, the first is that the meta data that is part of the image is only referenced by id; and the second is that there is no constructor for the PropertyItem Class.

This can make it tricky to identify what you are trying to change and seemingly impossible to add new meta data to an image.

The easiest thing to do for the first problem is to iterate through all the PropertyItems and to see for yourself what is in each one.

Dim i As System.Drawing.Image
i = Drawing.Image.FromFile(Server.MapPath("~/test.jpg"))
For Each p As System.Drawing.Imaging.PropertyItem In i.PropertyItems
Response.Write(p.Id & " " & p.Type & " " & Text.Encoding.Default.GetString(p.Value) & "<br>")
Next

This will list out all the properties a particular image has, here is an overview of what each type number is for:

  • 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.
  • 3 = Specifies that Value is an array of unsigned short (16-bit) integers.
  • 4 = Specifies that Value is an array of unsigned long (32-bit) integers.
  • 5 = Specifies that Value data member is an array of pairs of unsigned long integers. Each pair represents a fraction; the first integer is the numerator and the second integer is the denominator.
  • 6 = Specifies that Value is an array of bytes that can hold values of any data type.
  • 7 = Specifies that Value is an array of signed long (32-bit) integers.
  • 10 = Specifies that Value is an array of pairs of signed long integers. Each pair represents a fraction; the first integer is the numerator and the second integer is the denominator.

This information and more detail can be found here. Perhaps the most important thing to note about those types is number 2 (which is the type that most image titles are stored as) - it's a null terminated string, so when setting a new value you need to include the null terminator character Chr(0), failure to do this can lead to some unpredictable and probably undesirable results.

The second problem is that there is no constructor for the PropertyItem class, so you have to get an existing item and use that as a sort of pseudo-constructor (not sure how you would handle an image that had no properties at all).

Dim str As String = "hello world"
Dim pi As Drawing.Imaging.PropertyItem = i.GetPropertyItem(270)
pi.Id = 1
pi.Value = Text.Encoding.Default.GetBytes(str & Chr(0))
pi.Len = Len(str) + 1
i.SetPropertyItem(pi)
i.Save(Server.MapPath("~/test.jpg"))

So in the above example we got the property with id 270, then changed the id and the value and reset the length based on the new value + the null terminator character. Even though a type 2 property is listed as being ASCII, you appear to be able to get away with passing in non-ASCII charactered by using the default text encoder. Another thing to note, is that it appears that you cannot always change the type of the property when you are creating a new one, so you may have to find an image that does have the property type you want to use, create a property item from that image, then effectively transfer a copy of it to another image...

Post a comment

name:
(optional)

email:
(optional)

comment: