Results 1 to 2 of 2

Thread: how to add/update jpg keywords tag data

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    how to add/update jpg keywords tag data

    We currently have a large amount of images that we would like to add specific product name text to the image 'Keywords tag'. Currently the images do not have this info so will need to be added.

    Using the code below, if the current image doesn't have property I get the following:
    "Property cannot be found."

    Using Windows 7, if I manually add text to the image Properties > Details > Tags and then run the code, it will create a temp file but doesn't overwrite the Tags with what I enter into the txtAddTag textbox. This returns no error, just doesn't change.

    Is there a better way to do this?

    Code:
    'textbox: txtAddTag
    'button: Button1
    
    Imports System.Drawing.Imaging
    Imports System.Text
    Imports System.IO
    
    Private m_currImageFile As String
    Private m_currImageFileTemp As String
    
    Private Sub Form_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            ' delete temporary images that may exist from previous runs
            Try
                Dim di As DirectoryInfo = New DirectoryInfo("D:\temp\")
                Dim fi() As FileInfo = di.GetFiles("__exifEditor__*.*")
                For Each f As FileInfo In fi
                    File.Delete(f.FullName)
                Next
            Catch ex As Exception
                MessageBox.Show(("Failed to delete temporary files: " + ex.Message))
            End Try
        End Sub
    
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            If txtAddTag.Text.Trim.Length > 0 Then
                Try
                    UpdateImage()
                Catch ex As Exception
                    MessageBox.Show("Error: " & ex.ToString)
                End Try
            Else
                MessageBox.Show("Ooops.  Please enter a tag to add.")
            End If
    End Sub
    
    Sub UpdateImage()
    Try
    
                m_currImageFileTemp = ""
                m_currImageFile = "D:\temp\image-name.jpg"
                m_currImageFileTemp = (("D:\temp\__exifEditor__" + Path.GetRandomFileName))
                File.Copy(m_currImageFile, m_currImageFileTemp)
    
                Dim _Encoding As Encoding = Encoding.UTF8
                Dim theImage As Image = New Bitmap(m_currImageFileTemp)
                Dim propItem40094 As PropertyItem = theImage.GetPropertyItem(40094)
                propItem40094.Value = _Encoding.GetBytes((txtAddTag.Text + Microsoft.VisualBasic.ChrW(92)))
                theImage.SetPropertyItem(propItem40094)
    
                theImage.Save(m_currImageFile)
    
            Catch ex As Exception
                MessageBox.Show(("Error: " + ex.Message))
            End Try
    End Sub
    Other info I found and have used:

    http://www.codeproject.com/Articles/...for-JPG-images
    http://www.exiv2.org/tags.html

    tag (hex): 0x9c9e
    tag (dec): 40094
    IFD: Image
    Key: Exif.Image.XPKeywords
    Type: Byte
    Tag description: Keywords tag used by Windows, encoded in UCS2

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: how to add/update jpg keywords tag data

    Well part of your problem would appear to be that no such PropertyItem exists in the Image Class.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width