Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27360

VS 2010 how to add/update jpg keywords tag data

$
0
0
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

Viewing all articles
Browse latest Browse all 27360

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>