欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

利用FSO取得BMP,JPG,PNG,GIF文件信息

程序员文章站 2022-10-27 20:47:32
<% '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: '::::::::...
<%
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: bmp, gif, jpg and png :::
'::::原作:junyd:::::::::::
':::::翻译:欧阳东杰::::::::::::::::::::::::::::::::::::::::::
'::: :::
'::: 这个东东能从bmp, gif, jpg and png 图片拿到这个文件得字节 :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function getbytes(flnm, offset, bytes)
dim objfso
dim objftemp
dim objtextstream
dim lngsize
on error resume next
set objfso = createobject("scripting.filesystemobject")

' 首先,我们得到filesize
set objftemp = objfso.getfile(flnm)
lngsize = objftemp.size
set objftemp = nothing
fsoforreading = 1
set objtextstream = objfso.opentextfile(flnm, fsoforreading)
if offset > 0 then
strbuff = objtextstream.read(offset - 1)
end if
if bytes = -1 then ' get all!
getbytes = objtextstream.read(lngsize) 'readall
else
getbytes = objtextstream.read(bytes)
end if
objtextstream.close
set objtextstream = nothing
set objfso = nothing
end function

':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: :::
'::: 下面是把两个字节转化成统一数值的的功能 :::
'::: (小endian 和大的endian ) :::
'::: :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function lngconvert(strtemp)
lngconvert = clng(asc(left(strtemp, 1)) + ((asc(right(strtemp, 1)) * 256)))
end function
function lngconvert2(strtemp)
lngconvert2 = clng(asc(right(strtemp, 1)) + ((asc(left(strtemp, 1)) * 256)))
end function

':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: :::
'::: 这个功能做大多数真正的工作。 它意愿尝试 :::
'::: 读任何文件:::
'::: 如果它是一幅图表的图像,鉴定 . :::
'::: :::
'::: passed: :::
'::: flnm => filespec of file to read :::
'::: width => width of image :::
'::: height => height of image :::
'::: depth => color depth (in number of colors) :::
'::: strimagetype=> type of image (e.g. gif, bmp, etc.) :::
'::: :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function gfxspex(flnm, width, height, depth, strimagetype)
dim strpng
dim strgif
dim strbmp
dim strtype
strtype = ""
strimagetype = "(unknown)"
gfxspex = false
strpng = chr(137) & chr(80) & chr(78)
strgif = "gif"
strbmp = chr(66) & chr(77)
strtype = getbytes(flnm, 0, 3)
if strtype = strgif then ' is gif
strimagetype = "gif"
width = lngconvert(getbytes(flnm, 7, 2))
height = lngconvert(getbytes(flnm, 9, 2))
depth = 2 ^ ((asc(getbytes(flnm, 11, 1)) and 7) + 1)
gfxspex = true
elseif left(strtype, 2) = strbmp then ' is bmp
strimagetype = "bmp"
width = lngconvert(getbytes(flnm, 19, 2))
height = lngconvert(getbytes(flnm, 23, 2))
depth = 2 ^ (asc(getbytes(flnm, 29, 1)))
gfxspex = true
elseif strtype = strpng then ' is png
strimagetype = "png"
width = lngconvert2(getbytes(flnm, 19, 2))
height = lngconvert2(getbytes(flnm, 23, 2))
depth = getbytes(flnm, 25, 2)
select case asc(right(depth,1))
case 0
depth = 2 ^ (asc(left(depth, 1)))
gfxspex = true
case 2
depth = 2 ^ (asc(left(depth, 1)) * 3)
gfxspex = true
case 3
depth = 2 ^ (asc(left(depth, 1))) '8
gfxspex = true
case 4
depth = 2 ^ (asc(left(depth, 1)) * 2)
gfxspex = true
case 6
depth = 2 ^ (asc(left(depth, 1)) * 4)
gfxspex = true
case else
depth = -1
end select

else
strbuff = getbytes(flnm, 0, -1) ' get all bytes from file
lngsize = len(strbuff)
flgfound = 0
strtarget = chr(255) & chr(216) & chr(255)
flgfound = instr(strbuff, strtarget)
if flgfound = 0 then
exit function
end if
strimagetype = "jpg"
lngpos = flgfound + 2
exitloop = false
do while exitloop = false and lngpos < lngsize

do while asc(mid(strbuff, lngpos, 1)) = 255 and lngpos < lngsize
lngpos = lngpos + 1
loop
if asc(mid(strbuff, lngpos, 1)) < 192 or asc(mid(strbuff, lngpos, 1)) > 195 then
lngmarkersize = lngconvert2(mid(strbuff, lngpos + 1, 2))
lngpos = lngpos + lngmarkersize + 1
else
exitloop = true
end if
loop
'
if exitloop = false then
width = -1
height = -1
depth = -1
else
height = lngconvert2(mid(strbuff, lngpos + 4, 2))
width = lngconvert2(mid(strbuff, lngpos + 6, 2))
depth = 2 ^ (asc(mid(strbuff, lngpos + 8, 1)) * 8)
gfxspex = true
end if

end if
end function

':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: 测试:::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

' 为了测试,我们把文件放在c:\上
set objfso = createobject("scripting.filesystemobject")
set objf = objfso.getfolder("c:\")
set objfc = objf.files
response.write "<table border=""0"" cellpadding=""5"">"
for each f1 in objfc
if instr(ucase(f1.name), ".gif") then
response.write "<tr><td>" & f1.name & "</td><td>" & f1.datecreated & "</td><td>" & f1.size & "</td><td>"
if gfxspex(f1.path, w, h, c, strtype) = true then
response.write w & " x " & h & " " & c & " colors"
else
response.write " "
end if
response.write "</td></tr>"
end if
next
response.write "</table>"
set objfc = nothing
set objf = nothing
set objfso = nothing

%>