如何使用描述部分在 VB.NET 中读取图像标签

问题描述 投票:0回答:1

我正在尝试使用 VB.NET 从图像的描述部分提取图像标签。图片标签存储在描述部分:

enter image description here

我保存的标签:

StyleShootsModel=sam001;StyleShootsAspectRatio=2_3;StyleShootsPreset=Jacqueline;StyleShootsProducts=21FKA00012;StyleShootsFraming=CRP;StyleShootsEquipment=StyleShoots Live ();

我尝试了以下示例代码:

Public Shared Function GetDMetaTagFromImage(ByVal path As String) As DateTime
    Using fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read)
        Using myImage As Image = Image.FromStream(fs, False, False)
            Dim tagsValue As String = ""

            Dim tags = myImage.PropertyItems.FirstOrDefault(Function(pi) pi.Id = &H9C9E)
            If tags IsNot Nothing Then
                tagsValue = Encoding.Unicode.GetString(tags.Value)
            End If
            Return tagsValue
        End Using
    End Using
End Function

但是,代码似乎没有按预期工作。如何使用VB.NET从描述部分正确提取图像标签?

vb.net image
1个回答
0
投票
Imports System.Drawing.Imaging

Public Enum PropertyItemType As Short
    PropertyTagTypeByte = 1
    PropertyTagTypeASCII = 2
    PropertyTagTypeShort = 3
    PropertyTagTypeLong = 4
    PropertyTagTypeRational = 5
    PropertyTagTypeUndefined = 7
    PropertyTagTypeSLONG = 9
    PropertyTagTypeSRational = 10
End Enum

Public Shared Function GetTagsFromImage(ByVal path As String) As String
    Try
        Dim tags As String = ""
        Dim ExifUserComment As UShort = &H9C9E ' Use the appropriate PropertyItem ID for tags

        ' Open the image file
        Using myImage As Image = Image.FromFile(path)
            ' Get the PropertyItems collection
            Dim propItems As PropertyItem() = myImage.PropertyItems

            ' Find the PropertyItem with the appropriate ID
            Dim propItem As PropertyItem = propItems.FirstOrDefault(Function(pi) pi.Id = &H9C9E)

            If propItem IsNot Nothing Then
                ' Decode the property value and remove any null charactersa
                tags = Encoding.UTF8.GetString(propItem.Value).Replace(vbNullChar, "").Trim()
            Else
                tags = "Tags not found"
            End If
        End Using

        Return tags
    Catch ex As Exception
        Return "Error: " & ex.Message
    End Try
End Function

@jimi,我尝试根据@atricel共享的说明实现您提供的代码,但我仍然无法解析上面提到的元标记。

© www.soinside.com 2019 - 2024. All rights reserved.