2011-05-08 210 views

回答

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是一個全球性的字符串設定爲起始文件夾用於瀏覽並且記錄結果有Log子)。它生產基於圖像文件中的信息,例如結果:

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 

您也可以使用命令對象檢索的Windows值的屬性對話框中的值,但是這可能是有風險的,因爲不同的Windows版本,把他們涉及收集的不同點。

+0

感謝您的回答,但我無法提取其他信息,如關鍵字,照片的評分。我怎樣才能提取這些信息和信息內的情況? – nightfire001 2011-05-09 09:46:12

+0

有沒有辦法將信息寫入vb 6.0的圖像屬性? – nightfire001 2011-05-09 09:54:52

+0

像「評級」這樣的值不是圖像文件內部的圖像屬性,而是由Windows Shell與其他應用程序/查看者合作維護的。你需要通過Shell來獲取這些內容,但是編寫這樣的程序將很難在所有Windows版本上運行。 – Bob77 2011-05-09 20:05:28