
Tip: Reading the image dimensions of a GIF file
This question popped up several times, in the last weeks alone. I found
a solution in the meantime. It's so easy, you wouldn't believe it.
A GIF file is a pretty complicated thing. It can consist of several
images (in the case of an animated GIF), that need not even be all the
same size. However, there is the notion of a virtual screen on which all
images are painted. The image dimensions, as most people would think of
it, is precisely the dimensions of this screen.
These dimensions are stored right at the beginning of the GIF file,
right after a 6 byte signature (that marks it as a GIF file), as a
couple of 2-byte unsigned integers, in low-endian (Intel) order.
So getting the image dimensions out of a GIF file can be really dead
simple. This code will actually do just that (although, in practice, you
probably won't want the messagebox):
Type GIFheader
signature As String * 3
version As String * 3
width As Integer
height As Integer
End Type
Function GIFdimensions$ (ByVal filePath$)
Dim handle%: handle = FreeFile
Dim GIFheader As GIFheader
Open filePath For Binary Access Read As #handle
Get #handle, , GIFheader
Close #handle
If GIFheader.signature = "GIF" And _
(GIFheader.version = "87a" Or GIFheader.version = "89a") Then
GIFdimensions = CStr(GIFheader.width) & " x " _
& CStr(GIFheader.height)
Else
MsgBox "Not a GIF file",48,"ERROR"
End If
End Function
Bart.