Click here to Skip to main content
15,896,118 members
Articles / Programming Languages / Visual Basic

Metadata from Managed Code

Rate me:
Please Sign up or sign in to vote.
4.33/5 (7 votes)
18 Sep 2009CPOL5 min read 85.4K   2.2K   31  
a custom viewer that would make it easy for everyone to see a picture and view and edit the metadata.
Imports Microsoft.VisualBasic
Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports System.IO
Imports System.Text
Imports SHPropertyEdit

Public Class frmPropertyTest

    Dim FullFileName As String = String.Empty

    'Copyright (c) Microsoft Corporation.  All rights reserved.

    ''' <summary>
    ''' Display the OpenFileDialog and select file
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnBrowse_Click(ByVal sender As System.Object, _
                                    ByVal e As System.EventArgs) Handles btnBrowse.Click

        ClearFields()

        With OpenFileDialog

            .InitialDirectory = "C:\Development\VB\VB Code\vb.net8 Projects\PHOTO METADATA\# (RW) Property Edit (WinAPI)\Images"
            .Filter = "JPEG Files (*.jpg)|*.jpg|Audio Files (*.wma)|*.wma|All Files (*.*)|*.*"

            If .ShowDialog() = System.Windows.Forms.DialogResult.OK Then

                xtxteFileName.Text = .FileName
                FullFileName = .FileName

            End If

        End With

    End Sub

    ''' <summary>
    ''' Through repeated calls to GetProperty, return the values of desired properties.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>In this demo only five properties have been selected and tested</remarks>
    Private Sub btnGet_Click(ByVal sender As System.Object, _
                             ByVal e As System.EventArgs) Handles btnGet.Click

        txtStatus.Text = String.Empty

        If (FullFileName = String.Empty) Then

            txtStatus.Text = "First select a file..."

        Else

            Me.Cursor = Cursors.WaitCursor

            Using pe As ShellPropertyEdit = New ShellPropertyEdit()

                Dim rtnValue As String = String.Empty

                Dim rtnVal As Boolean = pe.GetProperty(FullFileName, lblSystemComment.Text)
                If rtnVal Then
                    txtSystemComment.Text = pe.PropertyValue
                Else
                    txtSystemComment.Text = String.Empty
                End If

                rtnVal = pe.GetProperty(FullFileName, lblSystemKeywords.Text)
                If rtnVal Then
                    txtSystemKeywords.Text = pe.PropertyValue
                Else
                    txtSystemKeywords.Text = String.Empty
                End If

                rtnVal = pe.GetProperty(FullFileName, lblSystemRating.Text)
                If rtnVal Then
                    txtSystemRating.Text = pe.PropertyValue
                Else
                    txtSystemRating.Text = String.Empty
                End If

                rtnVal = pe.GetProperty(FullFileName, lblSystemSubject.Text)
                If rtnVal Then
                    txtSystemSubject.Text = pe.PropertyValue
                Else
                    txtSystemSubject.Text = String.Empty
                End If

                rtnVal = pe.GetProperty(FullFileName, lblSystemTitle.Text)
                If rtnVal Then
                    txtSystemTitle.Text = pe.PropertyValue
                Else
                    txtSystemTitle.Text = String.Empty
                End If

                Me.Cursor = Cursors.Default

            End Using

        End If

    End Sub

    ''' <summary>
    ''' Through repeated calls to SetProperty, write the values of the desired properties.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnSet_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles btnSet.Click

        txtStatus.Text = String.Empty
        Dim rtnval As Boolean = False

        If (FullFileName = String.Empty) Then

            txtStatus.Text = "First select a file..."

        Else

            Me.Cursor = Cursors.WaitCursor

            Try

                Using pe As ShellPropertyEdit = New ShellPropertyEdit()

                    ' Set Comment
                    pe.PropertyValue = txtSystemComment.Text
                    rtnval = pe.SetProperty(FullFileName, _
                                            lblSystemComment.Text)
                    pe.PropertyValue = txtSystemKeywords.Text
                    rtnval = pe.SetProperty(FullFileName, _
                                            lblSystemKeywords.Text)
                    pe.PropertyValue = txtSystemRating.Text
                    rtnval = pe.SetProperty(FullFileName, _
                                            lblSystemRating.Text)
                    pe.PropertyValue = txtSystemSubject.Text
                    rtnval = pe.SetProperty(FullFileName, _
                                            lblSystemSubject.Text)
                    pe.PropertyValue = txtSystemTitle.Text
                    rtnval = pe.SetProperty(FullFileName, _
                                            lblSystemTitle.Text)
                End Using

            Catch ex As Exception

                DisplayErrorMessage(ex, "frmPropertyTest: SetProperty")

            End Try

            Me.Cursor = Cursors.Default

        End If

    End Sub

    ''' <summary>
    ''' Show information for a single property.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>
    ''' In this demo System.Comment is shown by default
    ''' </remarks>
    Private Sub btnInfo_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles btnInfo.Click

        Dim propertyName As String = txtFilter.Text

        txtStatus.Text = String.Empty

        If (propertyName = String.Empty) Then

            txtStatus.Text = "Enter a Property name in the filter textbox..."

        Else

            Me.Cursor = Cursors.WaitCursor

            Using pe As ShellPropertyEdit = New ShellPropertyEdit()

                Dim rtnVal As Boolean = pe.GetPropertyInfo(propertyName)
                If rtnVal Then

                    rtbResults.BringToFront()
                    rtbResults.Text = pe.PropertyInfo

                End If

                Me.Cursor = Cursors.Default

            End Using

        End If

    End Sub

    ''' <summary>
    ''' Get the values for all properties pertaining to the chosen file.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnEnum_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles btnEnum.Click

        Dim filter As String = txtFilter.Text
        txtStatus.Text = String.Empty

        If (FullFileName = String.Empty) Then

            txtStatus.Text = "Select a file..."

        Else

            Me.Cursor = Cursors.WaitCursor


            Using pe As ShellPropertyEdit = New ShellPropertyEdit()

                pe.docFileName = FullFileName
                '            Dim pe As New ShellPropertyEdit(FullFileName)
                Dim rtnVal As Boolean = pe.GetEnumProperties(filter)
                If rtnVal Then

                    lvResults.BringToFront()
                    PopulateLVResults(pe.aryPropertyEnumeration)

                Else

                    DisplayErrorMessage(pe.LastError, "frmPropertyTest: btnEnum_Click")

                End If

                Me.Cursor = Cursors.Default

            End Using

        End If

    End Sub

    Private Function PopulateLVResults(ByVal aryTags As String(,)) As Boolean

        Try

            Dim i As Int32
            Dim x As Int32 = UBound(aryTags, 2)

            For i = 0 To x

                Dim tagName As String = aryTags(0, i)

                ' Canonical Name
                Dim item As New ListViewItem(tagName)
                ' Display Name
                item.SubItems.Add(Trim(aryTags(1, i)))
                ' Attribute Value
                item.SubItems.Add(Trim(aryTags(2, i)))
                ' Display type
                item.SubItems.Add(Trim(aryTags(3, i)))
                ' Add to listview
                lvResults.Items.Add(item)

            Next

        Catch ex As Exception

            MsgBox("Exception:   " & ex.ToString & vbCrLf & vbCrLf & _
                   "Description: " & ex.Message & vbCrLf & vbCrLf & _
                   "Source:      frmMAtDemo: PopulateLVTags")

        End Try

    End Function

    Private Sub ClearFields()

        txtFilter.Text = String.Empty
        txtStatus.Text = String.Empty
        txtSystemComment.Text = String.Empty
        txtSystemKeywords.Text = String.Empty
        txtSystemRating.Text = String.Empty
        txtSystemSubject.Text = String.Empty
        txtSystemTitle.Text = String.Empty
        rtbResults.Clear()
        Me.lvResults.Items.Clear()

    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions