TypeImage




Public Type ImgDimType
height As Long
width As Long
End Type

Function getImgDim(ByVal fileName As String, ImgDim As ImgDimType, _
Ext As String) As Boolean
'

'declare vars

Dim handle As Integer, isValidImage As Boolean
Dim byteArr(255) As Byte, i As Integer
'

'init vars

isValidImage = False
ImgDim.height = 0
ImgDim.width = 0
'

'open file and get 256 byte chunk

handle = FreeFile
On Error Goto endFunction
Open fileName For Binary Access Read As #handle
Get handle, , byteArr
Close #handle
'

'check for jpg header (SOI): &HFF, &HD8

' contained in first 2 bytes


If byteArr(0) = &HFF And byteArr(1) = &HD8 Then isValidImage = True
Else
Goto checkGIF
End If
'

'check for SOF marker: &HFF, &HC0 TO &HCF


For i = 0 To 255
If byteArr(i) = &HFF And byteArr(i + 1) >= &HC0 _
And byteArr(i + 1) <= &HCF Then
ImgDim.height = byteArr(i + 5) * 256 + byteArr(i + 6)
ImgDim.width = byteArr(i + 7) * 256 + byteArr(i + 8)
'

'I think there's a bug in Planet Source

'Code's code parsing utility. This

'"Exit for" statement is wrongly

'indented as the "End If" statement,

'whereas the "End If" statement is

'recognized as the "Next i", thereby

'giving an uneven appearance to this

'code. I've unsuccessfully tried to

'eliminate this problem and its driving

'me nuts!

Exit For
End If
Next i
'get image type and exit

Ext = "jpg"
Goto endFunction
'

checkGIF:
'

'check for GIF header

If byteArr(0) = &H47 And byteArr(1) = &H49 And byteArr(2) = &H46 _
And byteArr(3) = &H38 Then
ImgDim.width = byteArr(7) * 256 + byteArr(6)
ImgDim.height = byteArr(9) * 256 + byteArr(8)
isValidImage = True
Else
Goto checkBMP
End If
'get image type and exit

Ext = "gif"
Goto endFunction
'

checkBMP:
'

'check for BMP header



If byteArr(0) = 66 And byteArr(1) = 77 Then
isValidImage = True
Else
Goto checkPNG
End If
'get record type info



If byteArr(14) = 40 Then

'get width and height of BMP

ImgDim.width = byteArr(21) * 256 ^ 3 + byteArr(20) * 256 ^ 2 _
+ byteArr(19) * 256 + byteArr(18)

ImgDim.height = byteArr(25) * 256 ^ 3 + byteArr(24) * 256 ^ 2 _
+ byteArr(23) * 256 + byteArr(22)
'another type of BMP

ElseIf byteArr(17) = 12 Then
'get width and height of BMP

ImgDim.width = byteArr(19) * 256 + byteArr(18)
ImgDim.height = byteArr(21) * 256 + byteArr(20)

End If
'get image type and exit

Ext = "bmp"
Goto endFunction
'

checkPNG:
'

'check for PNG header

If byteArr(0) = &H89 And byteArr(1) = &H50 And byteArr(2) = &H4E _
And byteArr(3) = &H47 Then
ImgDim.width = byteArr(18) * 256 + byteArr(19)
ImgDim.height = byteArr(22) * 256 + byteArr(23)
isValidImage = True
Else
Goto endFunction
End If
Ext = "png"
'

endFunction:
'

'return function's success status

getImgDim = isValidImage

End Function










( typeimage.html )- by Paolo Puglisi - Modifica del 17/12/2023