|
前几天见论坛上有人需要,批量获取硬盘内图像文件的尺寸(宽*高),
闲得无聊,写了一个获取图像尺寸的函数,在网络上的vb版,算是支持
格式比较全的了,目前支持以下文件格式png,jpg,bmp,gif,psd,tif(tiff),jp2,
其中bmp支持windows版本和IBM版本,tif支持PC版本和Mac版本
支持的文件即使扩展名错误也可正确识别,可输出正确的扩展名。- Type ImageSize
- P_Type As String
- Width As Long
- Height As Long
- End Type
- Function BinVal(bin) '低位在前Intel
- Dim i
- Dim ret: ret = 0
- For i = LenB(bin) To 1 Step -1
- ret = ret * 256 + AscB(MidB(bin, i, 1))
- Next
- BinVal = ret
- End Function
- Function BinVal2(bin) '高位在前Motorola
- Dim i
- Dim ret: ret = 0
- For i = 1 To LenB(bin)
- ret = ret * 256 + AscB(MidB(bin, i, 1))
- Next
- BinVal2 = ret
- End Function
- Function GetImageSize(fdata) As ImageSize
- Dim bFlag, fsize, ADOS, n&
- fsize = CLng(LenB(fdata)) '取得数据尺寸
- If fsize = 0 Then Exit Function
- Set ADOS = CreateObject("ADODB.Stream")
- With ADOS
- .Type = 1
- ' .Mode = 3
- .Open
- .LoadFromFile fdata
- .Position = 0
- '写文本对象读取图像长宽和类型
- .Position = 0 '重置数据开始位置
- bFlag = .Read(3)
- If IsNull(bFlag) Then
- GetImageSize.P_Type = "unknow"
- GetImageSize.Width = 0
- GetImageSize.Height = 0
- Exit Function
- End If
- '取文件类型和长宽
- Select Case Hex(BinVal(bFlag))
- Case "4E5089" 'png********************
- .Read (13)
- GetImageSize.P_Type = "png"
- GetImageSize.Width = BinVal2(.Read(4))
- GetImageSize.Height = BinVal2(.Read(4))
- Case "464947" 'gif*******************
- .Read (3)
- GetImageSize.P_Type = "gif"
- GetImageSize.Width = BinVal(.Read(2))
- GetImageSize.Height = BinVal(.Read(2))
- Case "504238" 'psd*********************
- .Read (13)
- GetImageSize.P_Type = "psd"
- GetImageSize.Height = BinVal2(.Read(2))
- .Read (2)
- GetImageSize.Width = BinVal2(.Read(2))
- Case "FFD8FF" 'jpg**********************
- Dim p1
- Do
- Do: p1 = BinVal(.Read(1)): Loop While p1 = 255 And Not .EOS
- If p1 > 191 And p1 < 196 Then Exit Do Else .Read (BinVal2(.Read(2)) - 2)
- Do: p1 = BinVal(.Read(1)): Loop While p1 < 255 And Not .EOS
- Loop While True
- .Read (3)
- GetImageSize.P_Type = "jpg"
- GetImageSize.Height = BinVal2(.Read(2))
- GetImageSize.Width = BinVal2(.Read(2))
- Case "4D4D", "2A4949" 'tif***************
- If Hex(BinVal(bFlag)) = "2A4949" Then
- .Position = 4
- .Position = BinVal(.Read(4)) + 2
- Do
- If BinVal(.Read(2)) = 256 Then Exit Do
- .Read (10)
- Loop
- n = (BinVal(.Read(2)) \ 2) * 2
- .Read (4)
- GetImageSize.P_Type = "tif"
- GetImageSize.Width = BinVal(.Read(n))
- .Read (12 - n)
- GetImageSize.Height = BinVal(.Read(n))
- Else
- .Position = 4
- .Position = BinVal2(.Read(4)) + 2
- Do
- If BinVal2(.Read(2)) = 256 Then Exit Do
- .Read (10)
- Loop
- n = (BinVal2(.Read(2)) \ 2) * 2
- .Read (4)
- GetImageSize.P_Type = "tif"
- GetImageSize.Width = BinVal2(.Read(n))
- .Read (12 - n)
- GetImageSize.Height = BinVal2(.Read(n))
- End If
- Case "0" '************jp2********************
- .Read (1)
- If Hex(BinVal(.Read(4))) = "2020506A" Then
- .Read (4)
- Do '定位图像框
- n = BinVal2(.Read(4))
- If Hex(BinVal(ADOS.Read(4))) = "6832706A" Then Exit Do
- If n = 1 Then
- n = BinVal2(.Read(8))
- .Read (n - 16)
- Else
- .Read (n - 8)
- End If
- Loop
- If n = 1 Then .Read (8)
- Do '定位图像头框
- n = BinVal2(.Read(4))
- If Hex(BinVal(ADOS.Read(4))) = "72646869" Then Exit Do
- If n = 1 Then
- n = BinVal2(.Read(8))
- .Read (n - 16)
- Else
- .Read (n - 8)
- End If
- Loop
- GetImageSize.P_Type = "jp2"
- GetImageSize.Height = BinVal2(.Read(4))
- GetImageSize.Width = BinVal2(.Read(4))
- End If
- Case Else 'bmp****************************
- If bFlag(0) = 66 And bFlag(1) = 77 Then
- .Read (11)
- If BinVal(.Read(4)) = 12 Then n = 2 Else n = 4 '区分Windows和O/S2
- GetImageSize.P_Type = "bmp"
- GetImageSize.Width = BinVal(.Read(n))
- GetImageSize.Height = BinVal(.Read(n))
- Else
- GetImageSize.P_Type = ""
- End If
- End Select
- .Close
- End With
- Set ADOS = Nothing
- Select Case GetImageSize.P_Type
- Case "png", "jpg", "bmp", "gif", "psd", "tif", "jp2"
- Case Else
- GetImageSize.Width = 0
- GetImageSize.Height = 0
- GetImageSize.P_Type = "unknow"
- End Select
- End Function
复制代码 以下是测试代码- Sub ttt()
- Dim st, im As ImageSize
- st = Application.GetOpenFilename("图片文件 (*.jp*;*.psd;*.tif*;*.png;*.gif;*.bmp), *.jp*;*.psd;*.tif*;*.png;*.gif;*.bmp")
- im = GetImageSize(st)
- MsgBox im.P_Type & "文件" & vbCrLf & "宽度:" & im.Width & vbCrLf & "高度:" & im.Height
- End Sub
复制代码 |
评分
-
3
查看全部评分
-
|