从图像文件中提取属性

时间:2011-05-08 13:52:33

标签: image vb6

有没有办法从vb 6.0中提取图像文件的属性?我想浏览特定的照片,然后从任何图像格式中提取以下属性。

enter image description here

1 个答案:

答案 0 :(得分:5)

如果你安装WIA 2.0(需要XP SP1,预装在Vista和Windows 7中),你可以这样做:

Private Sub Command1_Click()
    Dim imfSubject As WIA.ImageFile
    Dim vecProperty As WIA.Vector
    Dim propEach As WIA.Property

    With CommonDialog1
        .CancelError = True
        .DialogTitle = "Select JPEG Image"
        .Filter = "JPEG Image (*.jpg, *.jpeg)|*.jpg;*.jpeg|" _
                & "GIF Image (*.gif)|*.gif|" _
                & "PNG Image (*.png)|*.png"
        .FilterIndex = 1
        .Flags = cdlOFNExplorer _
              Or cdlOFNFileMustExist _
              Or cdlOFNLongNames _
              Or cdlOFNPathMustExist _
              Or cdlOFNShareAware
        .InitDir = strStartDir
        On Error Resume Next
        .ShowOpen
        If Err.Number = cdlCancel Then Exit Sub
        On Error GoTo 0

        Log "Photo " & .FileName, ClearLog:=True
        Log
    End With

    Set imfSubject = New WIA.ImageFile
    With imfSubject
        On Error Resume Next
        .LoadFile (CommonDialog1.FileName)
        If Err.Number <> 0 Then
            Log "Error &H" & Hex$(Err.Number) & " (" & CStr(Err.Number) & ") in " _
              & Err.Source
            Log Err.Description
            Err.Clear
            Exit Sub
        End If

        Log "Width = " & .Width
        Log "Height = " & .Height
        Log "Depth = " & .PixelDepth
        Log "HorizontalResolution = " & .HorizontalResolution
        Log "VerticalResolution = " & .VerticalResolution
        Log "FrameCount = " & .FrameCount

        If .IsIndexedPixelFormat Then
            Log "Pixel data contains palette indexes"
        End If

        If .IsAlphaPixelFormat Then
            Log "Pixel data has alpha information"
        End If

        If .IsExtendedPixelFormat Then
            Log "Pixel data has extended color information (16 bit/channel)"
        End If

        If .IsAnimated Then
            Log "Image is animated"
        End If

        For Each propEach In .Properties
            Select Case propEach.Name
                Case "40091"
                    Set vecProperty = propEach.Value
                    Log "Title = " & vecProperty.String

                Case "40092"
                    Set vecProperty = propEach.Value
                    Log "Comment = " & vecProperty.String

                Case "40093"
                    Set vecProperty = propEach.Value
                    Log "Author = " & vecProperty.String

                Case "40094"
                    Set vecProperty = propEach.Value
                    Log "Keywords = " & vecProperty.String

                Case "40095"
                    Set vecProperty = propEach.Value
                    Log "Subject = " & vecProperty.String

                Case Else
                    Log propEach.Name & " = " & CStr(propEach.Value)
            End Select
        Next
    End With
End Sub

代码假定strStartDir是一个全局String,设置为一个用于浏览的起始文件夹,并且有一个Log sub用于记录结果)。它根据图像文件中的信息生成结果,例如:

Photo C:\Users\George\Pictures\Phone\IMAG0005.jpg

Width = 1600
Height = 1200
Depth = 24
HorizontalResolution = 96
VerticalResolution = 96
FrameCount = 1
EquipMake = HTC
EquipModel = VOGU100
XResolution = 72
YResolution = 72
ResolutionUnit = 2
DateTime = 2010:05:17 11:54:38
Artist = Bob Riemersma
ExifDTOrig = 2010:05:17 11:54:38
ExifFlash = 0
ExifPixXDim = 1600
ExifPixYDim = 1200
ExifColorSpace = -1
ExifDTDigitized = 2010:05:17 11:54:38
ThumbnailImageWidth = 160
ThumbnailImageHeight = 120
ThumbnailCompression = 6
JPEGInterFormat = 368

您还可以使用Shell对象检索这些属性对话框值的Windows值,但这可能是一个废话,因为不同的Windows版本将它们放在所涉及的集合中的不同位置。