从图像文件中提取属性
答
如果安装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版本,把他们涉及收集的不同点。
感谢您的回答,但我无法提取其他信息,如关键字,照片的评分。我怎样才能提取这些信息和信息内的情况? – nightfire001 2011-05-09 09:46:12
有没有办法将信息写入vb 6.0的图像属性? – nightfire001 2011-05-09 09:54:52
像“评级”这样的值不是图像文件内部的图像属性,而是由Windows Shell与其他应用程序/查看者合作维护的。你需要通过Shell来获取这些内容,但是编写这样的程序将很难在所有Windows版本上运行。 – Bob77 2011-05-09 20:05:28