user2994429
user2994429

Reputation: 105

VB.NET Retrieving Meta Tags from an Image

Alright I've been messing with this for a few days now, and I found this topic here on the board.

VB.NET Renaming File and Retagging / Edit Image MetaData / Meta Tags

After reading through that, and getting the library that one of the answers spoke of, I easily inserted the code into my project and everything was working decently well. However the entire reason I was looking for such a thing was to get the 'Keywords/Tags' metadata, and this code no matter what I did wouldn't give me what I was looking for.

Knowing that the 'Keywords' address was &H9C9E I was able to do the following.

    Dim test As String = EX.GetPropertyString(&H9C9E)
    MsgBox(test)

That worked to a point, I messed with it for awhile but no matter what I changed I was unable to get that to display anymore than the very first character of the tags. For example I placed 'Test' in the keywords and it simply returned 'T'. I looked through the coding of the library, but I wasn't able to figure out why it wasn't returning everything. I've tried changing the content in tags in an attempt at getting it to load, and I've tried with a few different images though it always returned only the first character.

Also when I attempted to make changes to the keywords data, I was unable to get it to display anything even though the following code worked with everything else.

    EX.SetPropertyString(ExifWorks.TagNames.ExifUserComment, "This Worked")
    EX.GetBitmap.Save(SaveLoc + "1decfabd90e2355eac81b3f9735e10e3.jpg")

So my question is, why is it only displaying the very first character and why isn't it able to save to the address?

EDIT:

After playing with it a bit I was able to get the contents of 'Keywords' to appear, though it isn't exactly ideal.

    Dim test = EX.GetProperty(&H9C9E)
    Dim test2 = EX.Encoding.GetCharCount(test)
    Dim test3 = EX.Encoding.GetChars(test)

    Dim tolnum As Integer = test2
    Dim cur As Integer = -1

    While tolnum > 0
        cur = cur + 1
        tolnum = tolnum - 1
        MsgBox(test3(cur))
    End While

For example with 'TEST' in the 'Keywords' I get 'T;E;S;T;;;;' ; Is marking the unknown characters/blank msgboxes.

Upvotes: 0

Views: 3003

Answers (1)

user2994429
user2994429

Reputation: 105

Alright with the help of Grim I've been able to correct the issue and find an answer. This is the solution I used to solve my issue.

Since the 'Keywords' field is Unicode I had to ensure that when reading the information from the field it was Encoded with Unicode, and when saving I needed to ensure it was converted to Unicode before saving.

The Code:

Sub Test()

    ''Get SaveLoc from settings
    Dim SaveLoc As String
    SaveLoc = My.Settings.SaveLocation

    ''Select photo to edit/view
    Dim Bitmap As String
    Bitmap = SaveLoc + "\Test\" + "1decfabd90e2355eac81b3f9735e10e3.jpg"

    ''Use ExifWorks library
    Dim EX As New ExifWorks(Bitmap)

    ''Set Encoding to Unicode
    Dim enc As New System.Text.UnicodeEncoding

    ''Get data from Keywords field
    Dim keywords = EX.GetProperty(&H9C9E)

    ''Use Unicode encoding to get the contents currently within Keywords
    Dim decode = enc.GetChars(keywords)

    ''View the current tags, decoded from Unicode
    MsgBox(decode)

    ''Set Encoding to Unicode
    Dim enc2 As Encoding = Encoding.Unicode

    ''Tags to Add
    Dim tags As String = "These; are; testing; tags"

    ''Encode to Unicode and switch to Bytes
    Dim Data() As Byte = enc2.GetBytes(tags & vbNullChar)

    ''Append the Keywords field with the new tags
    EX.SetProperty(&H9C9E, Data, ExifWorks.ExifDataTypes.UnsignedByte)

    ''Save the changes
    EX.GetBitmap.Save(SaveLoc + "\Test\" + "NEW1decfabd90e2355eac81b3f9735e10e3.jpg")

End Sub

Upvotes: 1

Related Questions