復制代碼 代碼如下:
title>LoadPicture函數/title>
form name="frm">
選擇圖片input type="file" name="pic" onChange="GetPicInfor()" >
/form>
script language="vbscript">
Sub GetPicInfor()
dim objpic,iWidth,iHeight
dim pictype,picpath
picpath=document.frm.pic.value
set objpic=Loadpicture(picpath)
iWidth = round(objpic.width / 26.4583) '26.4583是像素值
iHeight = round(objpic.height / 26.4583)
select case objpic.type
case 0
pictype = "None"
case 1
pictype = "Bitmap"
case 2
pictype = "Metafile"
case 3
pictype = "Icon"
case 4
pictype = "Win32-enhanced metafile"
end select
document.write "你選擇了圖片"picpath
document.write "li>長度:"iHeight"/li>"
document.write "li>寬度:"iwidth"/li>"
document.write "li>類型:"pictype"/li>"
End Sub
/script>
不過這個函數有個漏洞,可以探測電腦上存在的文件名。2004年的漏洞,微軟現在也沒補,示例:
復制代碼 代碼如下:
form onsubmit="doIt(this);return false">
input name="filename" value="c:\boot.ini" size="80" type="text">input type="submit">
/form>
script language="vbscript">
Sub loadIt(filename)
LoadPicture(filename)
End Sub
/script>
script language="javascript">
function doIt(form) {
try {
loadIt(form.filename.value);
} catch(e) {
result = e.number;
}
if (result != -2146827856) {
alert('file exists');
} else {
alert('file does not exist');
}
}
/script>
這段代碼中有一個“魔法數字(Magic Number)”26.4583,曾經有位昵稱是“亂碼”的朋友問過我這個26.4583是怎么來的,當時我也不知道。
前段時間逆向分析了一下vbscript.dll,才發現了其中的奧秘:
復制代碼 代碼如下:
26.4583 = 2540 / 96
那你一定要問,這個2540和96又是怎么來的?
要弄清楚這個問題,首先要知道VBS的LoadPicture函數返回的到底是什么,VBS文檔是這么描述LoadPicture函數的:
Returns a picture object. Available only on 32-bit platforms.
只說返回圖片對象,卻沒說該圖片對象有什么屬性和方法。文檔語焉不詳,只好動用OllyDbg了:

LoadPicture函數內部調用了OleLoadPicture函數,查文檔可知返回的是IPictureDisp接口。不過后來我發現了更簡單的方法,那就是查VB的函數聲明(誰讓它們是一家人呢),在VB的對象瀏覽器中查找LoadPicture函數:
Function LoadPicture([FileName], [Size], [ColorDepth], [X], [Y]) As IPictureDisp雖然VBS的LoadPicture函數比VB的簡單,但是返回值應該是一樣的。
好了,知道返回的是IPictureDisp接口,文檔說它支持下面的屬性:
Property |
Type |
Access |
Description |
Handle |
OLE_HANDLE (int) |
R |
The Windows GDI handle of the picture |
hPal |
OLE_HANDLE (int) |
RW |
The Windows handle of the palette used by the picture. |
Type |
short |
R |
The type of picture (see PICTYPE). |
Width |
OLE_XSIZE_HIMETRIC (long) |
R |
The width of the picture. |
Height |
OLE_YSIZE_HIMETRIC (long) |
R |
The height of the picture. |
我們只關心Width和Height,它們分別表示圖片的寬和高,但是它們的單位不是像素(Pixel),而是Himetric,我們要做的是把Himetric換算成Pixel。
首先把Himetric換算成英寸(Inch),1 Himetric = 0.01 mm,1 Inch = 2.54 cm,所以1 Inch = 2540 Himetric。
然后從Inch換算成Pixel,1 Inch等于多少Pixel呢?這是由系統的DPI(Dot Per Inch)設置決定的,默認值是96。
現在知道2540和96是怎么來的了吧?不過上面的代碼存在兩個問題:第一,使用了2540/96的近似值,可能會有誤差;第二,使用了DPI的默認值96,而DPI的值是可以在控制面板中修改的。
VBS中LoadPicture函數的正確用法是:
復制代碼 代碼如下:
Option Explicit
'By Demon
Dim p
Set p = LoadPicture("D:\test.jpg")
WScript.Echo "Width: " Himetric2Pixel(p.Width)
WScript.Echo "Height: " Himetric2Pixel(p.Height)
Function Himetric2Pixel(n)
'1 Inch = 2540 Himetric
Const key = "HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics\AppliedDPI"
Dim WshShell, dpi
Set WshShell = WScript.CreateObject("Wscript.Shell")
dpi = WshShell.RegRead(key)
Himetric2Pixel = Round(n * dpi / 2540)
End Function
Windows 7下通過測試,其他系統中獲取DPI的方法可能會不同,請自行修改。
上面修正的內容來自: http://demon.tw/programming/vbs-loadpicture.html
您可能感興趣的文章:- vbs中的LoadPicture函數示例
- VBS教程:函數-LoadPicture 函數