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

  1   
  2':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
  3':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
  4'::: BMP, GIF, JPG and PNG :::   
  5':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
  6':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
  7'::: :::   
  8'::: This function gets a specified number of bytes from any :::   
  9'::: file, starting at the offset (base 1) :::   
 10'::: :::   
 11'::: Passed: :::   
 12'::: flnm => Filespec of file to read :::   
 13'::: offset => Offset at which to start reading :::   
 14'::: bytes => How many bytes to read :::   
 15'::: :::   
 16':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
 17function GetBytes(flnm, offset, bytes)   
 18Dim objFSO   
 19Dim objFTemp   
 20Dim objTextStream   
 21Dim lngSize   
 22on error resume next   
 23Set objFSO = CreateObject("Scripting.FileSystemObject")   
 24  
 25' First, we get the filesize   
 26Set objFTemp = objFSO.GetFile(flnm)   
 27lngSize = objFTemp.Size   
 28set objFTemp = nothing   
 29fsoForReading = 1   
 30Set objTextStream = objFSO.OpenTextFile(flnm, fsoForReading)   
 31if offset > 0 then   
 32strBuff = objTextStream.Read(offset - 1)   
 33end if   
 34if bytes = -1 then ' Get All!   
 35GetBytes = objTextStream.Read(lngSize) 'ReadAll   
 36else   
 37GetBytes = objTextStream.Read(bytes)   
 38end if   
 39objTextStream.Close   
 40set objTextStream = nothing   
 41set objFSO = nothing   
 42end function   
 43  
 44':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
 45'::: :::   
 46'::: Functions to convert two bytes to a numeric value (long) :::   
 47'::: (both little-endian and big-endian) :::   
 48'::: :::   
 49':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
 50function lngConvert(strTemp)   
 51lngConvert = clng(asc(left(strTemp, 1)) + ((asc(right(strTemp, 1)) * 256)))   
 52end function   
 53function lngConvert2(strTemp)   
 54lngConvert2 = clng(asc(right(strTemp, 1)) + ((asc(left(strTemp, 1)) * 256)))   
 55end function   
 56  
 57':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
 58'::: :::   
 59'::: This function does most of the real work. It will attempt :::   
 60'::: to read any file, regardless of the extension, and will :::   
 61'::: identify if it is a graphical image. :::   
 62'::: :::   
 63'::: Passed: :::   
 64'::: flnm => Filespec of file to read :::   
 65'::: width => width of image :::   
 66'::: height => height of image :::   
 67'::: depth => color depth (in number of colors) :::   
 68'::: strImageType=> type of image (e.g. GIF, BMP, etc.) :::   
 69'::: :::   
 70':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
 71function gfxSpex(flnm, width, height, depth, strImageType)   
 72dim strPNG   
 73dim strGIF   
 74dim strBMP   
 75dim strType   
 76strType = ""   
 77strImageType = "(unknown)"   
 78gfxSpex = False   
 79strPNG = chr(137) & chr(80) & chr(78)   
 80strGIF = "GIF"   
 81strBMP = chr(66) & chr(77)   
 82strType = GetBytes(flnm, 0, 3)   
 83if strType = strGIF then ' is GIF   
 84strImageType = "GIF"   
 85Width = lngConvert(GetBytes(flnm, 7, 2))   
 86Height = lngConvert(GetBytes(flnm, 9, 2))   
 87Depth = 2 ^ ((asc(GetBytes(flnm, 11, 1)) and 7) + 1)   
 88gfxSpex = True   
 89elseif left(strType, 2) = strBMP then ' is BMP   
 90strImageType = "BMP"   
 91Width = lngConvert(GetBytes(flnm, 19, 2))   
 92Height = lngConvert(GetBytes(flnm, 23, 2))   
 93Depth = 2 ^ (asc(GetBytes(flnm, 29, 1)))   
 94gfxSpex = True   
 95elseif strType = strPNG then ' Is PNG   
 96strImageType = "PNG"   
 97Width = lngConvert2(GetBytes(flnm, 19, 2))   
 98Height = lngConvert2(GetBytes(flnm, 23, 2))   
 99Depth = getBytes(flnm, 25, 2)   
100select case asc(right(Depth,1))   
101case 0   
102Depth = 2 ^ (asc(left(Depth, 1)))   
103gfxSpex = True   
104case 2   
105Depth = 2 ^ (asc(left(Depth, 1)) * 3)   
106gfxSpex = True   
107case 3   
108Depth = 2 ^ (asc(left(Depth, 1))) '8   
109gfxSpex = True   
110case 4   
111Depth = 2 ^ (asc(left(Depth, 1)) * 2)   
112gfxSpex = True   
113case 6   
114Depth = 2 ^ (asc(left(Depth, 1)) * 4)   
115gfxSpex = True   
116case else   
117Depth = -1   
118end select   
119  
120else   
121strBuff = GetBytes(flnm, 0, -1) ' Get all bytes from file   
122lngSize = len(strBuff)   
123flgFound = 0   
124strTarget = chr(255) & chr(216) & chr(255)   
125flgFound = instr(strBuff, strTarget)   
126if flgFound = 0 then   
127exit function   
128end if   
129strImageType = "JPG"   
130lngPos = flgFound + 2   
131ExitLoop = false   
132do while ExitLoop = False and lngPos < lngSize   
133  
134do while asc(mid(strBuff, lngPos, 1)) = 255 and lngPos < lngSize   
135lngPos = lngPos + 1   
136loop   
137if asc(mid(strBuff, lngPos, 1)) < 192 or asc(mid(strBuff, lngPos, 1)) > 195 then   
138lngMarkerSize = lngConvert2(mid(strBuff, lngPos + 1, 2))   
139lngPos = lngPos + lngMarkerSize + 1   
140else   
141ExitLoop = True   
142end if   
143loop   
144'   
145if ExitLoop = False then   
146Width = -1   
147Height = -1   
148Depth = -1   
149else   
150Height = lngConvert2(mid(strBuff, lngPos + 4, 2))   
151Width = lngConvert2(mid(strBuff, lngPos + 6, 2))   
152Depth = 2 ^ (asc(mid(strBuff, lngPos + 8, 1)) * 8)   
153gfxSpex = True   
154end if   
155  
156end if   
157end function   
158  
159':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
160'::: Test Harness :::   
161':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::   
162  
163' To test, we'll just try to show all files with a .GIF extension in the root of C:   
164Set objFSO = CreateObject("Scripting.FileSystemObject")   
165Set objF = objFSO.GetFolder("c:\")   
166Set objFC = objF.Files   
167response.write "

<table 0""="" 5""="" border="" cellpadding="">"
For Each f1 in objFC
if instr(ucase(f1.Name), ".GIF") then
response.write "<tr><td>" &amp; f1.name &amp; "</td><td>" &amp; f1.DateCreated &amp; "</td><td>" &amp; f1.Size &amp; "</td><td>"
if gfxSpex(f1.Path, w, h, c, strType) = true then
response.write w &amp; " x " &amp; h &amp; " " &amp; c &amp; " colors"
else
response.write " "
end if
response.write "</td></tr>"
end if
Next
response.write "</table>

1"   
2set objFC = nothing   
3set objF = nothing   
4set objFSO = nothing   
5  
Published At
Categories with Web编程
Tagged with
comments powered by Disqus