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