FSO读取BMP,JPG,PNG,GIF图像文件信息的函数

作者:佚名 来源:蓝色理想 时间:2007-08-04 09:56:00 

利用FSO取得BMP,JPG,PNG,GIF文件信息:大小,宽、高尺寸等 


’:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
’:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
’::: BMP, GIF, JPG and PNG :::
’:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
’:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
’::: :::
’::: This function gets a specified number of bytes from any :::
’::: file, starting at the offset (base 1) :::
’::: :::
’::: Passed: :::
’::: flnm => Filespec of file to read :::
’::: offset => Offset at which to start reading :::
’::: bytes => How many bytes to read :::
’::: 翻译注释:asp之家 www.aspxhome.com
’水平有限很多没看懂,不好意思,呵呵:
’::: 功能:获取文件大小,
’::: 参数:flnm=>文件路径,offset=>好像这个没什么意义,bytes =>读取指定大小
’:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function GetBytes(flnm, offset, bytes)
Dim objFSO
Dim objFTemp
Dim objTextStream
Dim lngSize
on error resume next
Set objFSO = CreateObject("Scripting.FileSystemObject")
’ 首先,我们获取文件尺寸
Set objFTemp = objFSO.GetFile(flnm)
’GetFile返回一个和指定路径中文件相对应的 File 对象
lngSize = objFTemp.Size
set objFTemp = nothing
fsoForReading = 1
’以只读模式打开文件。不能对此文件进行写操作。
Set objTextStream = objFSO.OpenTextFile(flnm, fsoForReading)
’OpenTextFile打开指定的文件并返回一个 TextStream 对象,可以读取、写入此对象或将其追加到文件
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)
’读取并返回指定bytes的字符数
end if
objTextStream.Close
set objTextStream = nothing
set objFSO = nothing
end function 



’:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
’::: :::
’::: Functions to convert two bytes to a numeric value (long) :::
’::: (both little-endian and big-endian) :::
’::: :::
’:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function lngConvert(strTemp)
lngConvert = clng(asc(left(strTemp, 1)) + ((asc(right(strTemp, 1)) * 256)))
’asc()返回与字符串的第一个字母对应的 ANSI 字符代码,如a是97
end function
function lngConvert2(strTemp)
lngConvert2 = clng(asc(right(strTemp, 1)) + ((asc(left(strTemp, 1)) * 256)))
end function



’:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
’::: :::
’::: This function does most of the real work. It will attempt :::
’::: to read any file, regardless of the extension, and will :::
’::: identify if it is a graphical image. :::
’::: :::读取所有图形文件
’::: 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



<%
’:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
’::: Test Harness :::测试例子
’:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
’ To test, we’ll just try to show all files with a .GIF extension in the root of C:
’读取c盘的所有扩展名是GIF的图像文件,
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 & " 色"
else
response.write " "
end if
response.write "</td></tr>"
end if
Next
response.write "</table>"
set objFC = nothing
set objF = nothing
set objFSO = nothing
%>


标签:bmp,jpg,gif,fso,读取
0
投稿

猜你喜欢

  • 2008年情人节各网站节日Logo欣赏

    2008-02-17 14:12:00
  • 如何在网页设计中使用个性化字体

    2009-07-07 11:29:00
  • Oracle 创建用户及数据表的方法

    2009-09-26 18:25:00
  • Oracle 数组的学习 小知识也要积累,养成好的学习态度

    2009-08-04 12:42:00
  • 记录下两个正则表达式的使用

    2009-11-30 12:56:00
  • 关于AJAX缓存数据

    2008-03-26 12:11:00
  • css学习笔记:为什么在IE6下图片会往下掉一行

    2009-06-19 12:50:00
  • ASP 错误代码

    2009-05-11 12:38:00
  • 如何保持Oracle数据库的优良性能

    2009-02-26 10:39:00
  • JavaScript奇怪的比较——隐式类型转换

    2009-02-15 13:06:00
  • 百度在线手写输入法

    2010-02-03 14:27:00
  • 在JScript中使用RecordSet对象的GetRows方法

    2008-01-16 13:12:00
  • 使用MySQL数据库的23个注意事项

    2010-03-18 15:46:00
  • 胜过语言的图形符号

    2009-05-06 12:43:00
  • 巧用XSL和ASP在线编辑XML文档

    2008-10-25 14:54:00
  • WEB2.0网页制作标准教程(1)选择什么样的DOCTYPE

    2007-11-13 12:57:00
  • 关于select元素的两个小问题

    2008-12-25 13:41:00
  • ASP 相关文章或者相关产品

    2011-03-30 11:12:00
  • Web UI 设计(网页设计)命名规范

    2009-05-13 13:06:00
  • 文字超长自动省略,以...代替,CSS实现

    2009-07-16 10:15:00
  • asp之家 网络编程 m.aspxhome.com