Friday, August 17, 2012

EXIF Extraction

So, like any good programmer, I have the camel on my bookshelf where the allmighty Larry asserts (p. 609) that laziness is the first great virtue of a programmer.  Not to be a virtue-less programmer, I was off the teh googlez to search for an EXIF library.

I figured that it's bound to have come up before and lo and behold I found many a library.  Most did pretty much what I wanted, but this is for a web app and every single library left a lock on the file after it was done executing.  I guess I could have restarted the app pool after every page load, but that seemed a bit excessive.

I poked around MSDN and saw that it wasn't that difficult to read it by myself.

So, I wrote my own chunk of code:


b = new System.Drawing.Bitmap(fileInfo.FullName)
  Dim encoding As New System.Text.ASCIIEncoding()
  for each pi in b.PropertyItems
    if pi.ID = 40091 then ' I only need the title
      description = System.Text.Encoding.Unicode.GetString(pi.value)
      exit for
    end if
  Next


and viola, EXIF data.

Sidebar here:  I am a c# guy.  I cannot stand using vb.  but, DotNetNuke is written in vb so, I don't have much of a choice (I'm not rolling a new class for 15 lines of code)

But, with the EXIF data came the aforementioned file lock.

Back to MSDN to poke around at the Image Class.  Skip down a bit and lookie lookie it implements IDisposible.  Oh boy, is it really this easy?

yes, in fact it is.


using b = new System.Drawing.Bitmap(fileInfo.FullName)
  Dim encoding As New System.Text.ASCIIEncoding()
  for each pi in b.PropertyItems
    if pi.ID = 40091 then
      description = System.Text.Encoding.Unicode.GetString(pi.value)
      exit for
    end if
  Next
End Using

Game, set, match.

1 comment:

  1. Hi,Successfully meets business targets and models *Keeps customers coming back for more! Vanessa Donnelly is a software designer and Ease of Use Consultant within IBM specializing in Web Design Cochin and Content Management.Thanks............

    ReplyDelete