'i have released this source code into the public domain. you may use it
'with no strings attached.
'just call getimagesize with a string containing the filename, and
'it will return a user defined type 'imagesize' (see below)
'return values of 0 indicate an error of some sort. the error handling
'in this module is limited. there is *no* error handling on the test
'form. this routine is limited to x or y sizes of 32767 pixels, but that
'should not be a problem.
'check back athttp://qtm.net/~davidc
'i may add support for more file types.
'supported in this version:
'jpeg
'gif
'png
'this routine does not require any royalty fees for unisys as it
'does nothing with the compressed part of gif files. it simply reads
'4 bytes to determine image size.
option explicit
public wimg as long
public himg as long
public type imagesize
width as long
height as long
end type
public sub getimagesize(sfilename as string)
on error resume next 'you'll want to change this
dim ifn as integer
dim btemp(3) as byte
dim lflen as long
dim lpos as long
dim bhmsb as byte
dim bhlsb as byte
dim bwmsb as byte
dim bwlsb as byte
dim bbuf(7) as byte
dim bdone as byte
dim icount as integer
lflen = filelen(sfilename)
ifn = freefile
open sfilename for binary as ifn
get #ifn, 1, btemp()
'png file
if btemp(0) = &h89 and btemp(1) = &h50 and btemp(2) = &h4e _
and btemp(3) = &h47 then
get #ifn, 19, bwmsb
get #ifn, 20, bwlsb
get #ifn, 23, bhmsb
get #ifn, 24, bhlsb
'getimagesize.width = combinebytes(bwlsb, bwmsb)
'getimagesize.height = combinebytes(bhlsb, bhmsb)
wimg = combinebytes(bwlsb, bwmsb)
himg = combinebytes(bhlsb, bhmsb)
end if
'gif file
if btemp(0) = &h47 and btemp(1) = &h49 and btemp(2) = &h46 _
and btemp(3) = &h38 then
get #ifn, 7, bwlsb
get #ifn, 8, bwmsb
get #ifn, 9, bhlsb
get #ifn, 10, bhmsb
'getimagesize.width = combinebytes(bwlsb, bwmsb)
'getimagesize.height = combinebytes(bhlsb, bhmsb)
wimg = combinebytes(bwlsb, bwmsb)
himg = combinebytes(bhlsb, bhmsb)
end if
'jpeg file
if btemp(0) = &hff and btemp(1) = &hd8 and btemp(2) = &hff then
debug.print "jpeg"
lpos = 3
do
do
get #ifn, lpos, bbuf(1)
get #ifn, lpos + 1, bbuf(2)
lpos = lpos + 1
loop until (bbuf(1) = &hff and bbuf(2) <> &hff) or lpos > lflen
for icount = 0 to 7
get #ifn, lpos + icount, bbuf(icount)
next icount
if bbuf(0) >= &hc0 and bbuf(0) <= &hc3 then
bhmsb = bbuf(4)
bhlsb = bbuf(5)
bwmsb = bbuf(6)
bwlsb = bbuf(7)
bdone = 1
else
lpos = lpos + (combinebytes(bbuf(2), bbuf(1))) + 1
end if
loop while lpos < lflen and bdone = 0
'getimagesize.width = combinebytes(bwlsb, bwmsb)
'getimagesize.height = combinebytes(bhlsb, bhmsb)
wimg = combinebytes(bwlsb, bwmsb)
himg = combinebytes(bhlsb, bhmsb)
end if
close ifn
end sub
private function combinebytes(lsb as byte, msb as byte) as long
combinebytes = clng(lsb + (msb * 256))
end function
该文章在 2010/7/7 23:58:28 编辑过