这可能会有所帮助 - 使用 VBScript 将图像文件编码为 base64 的 HTA(代码改编自VBScript和VBScript 中的Base64 Encode String以打开对话框以选择文件路径)。
您可以生成 base64 代码并将其用作图像的来源,例如:
<img src="data:image/png;base64, [base64 code inserted here] ">
<!DOCTYPE html>
<html>
<head>
<HTA:APPLICATION
ID="oHta"
APPLICATIONNAME="Base64 Encode"
ICON="favicon.ico"
/>
<LINK id=shortcutlink REL="SHORTCUT ICON" HREF="favicon.ico">
<META http-equiv="x-ua-compatible" content="text/html; charset=utf-8">
<TITLE>Base64 Encoder</TITLE>
</head>
<script language=vbscript>
Function fBase64Encode(sourceStr)
Dim rarr()
carr = Array( "A", "B", "C", "D", "E", "F", "G", "H", _
"I", "J", "K", "L", "M", "N", "O" ,"P", _
"Q", "R", "S", "T", "U", "V", "W", "X", _
"Y", "Z", "a", "b", "c", "d", "e", "f", _
"g", "h", "i", "j", "k", "l", "m", "n", _
"o", "p", "q", "r", "s", "t", "u", "v", _
"w", "x", "y", "z", "0", "1", "2", "3", _
"4", "5", "6", "7", "8", "9", "+", "/")
n = Len(sourceStr)-1
ReDim rarr(n\3)
For i=0 To n Step 3
a = Asc(Mid(sourceStr,i+1,1))
If i < n Then
b = Asc(Mid(sourceStr,i+2,1))
Else
b = 0
End If
If i < n-1 Then
c = Asc(Mid(sourceStr,i+3,1))
Else
c = 0
End If
rarr(i\3) = carr(a\4) & carr((a And 3) * 16 + b\16) & carr((b And 15) * 4 + c\64) & carr(c And 63)
Next
i = UBound(rarr)
If n Mod 3 = 0 Then
rarr(i) = Left(rarr(i),2) & "=="
ElseIf n Mod 3 = 1 Then
rarr(i) = Left(rarr(i),3) & "="
End If
fBase64Encode = Join(rarr,"")
End Function
'-------------------------------------------------------------------------------
function fBase64Decode(str)
fBase64Decode = ""
table = fGenerateBase64Table
bits = 0
for x = 1 to len(str) step 1
c = table(1+asc(mid(str,x,1)))
if (c <> -1) then
if (bits = 0) then
outword = c*4
bits = 6
elseif (bits = 2) then
outword = c+outword
strBase64 = strBase64 & chr(clng("&H" & hex(outword mod 256)))
bits = 0
elseif (bits = 4) then
outword = outword + int(c/4)
strBase64 = strBase64 & chr(clng("&H" & hex(outword mod 256)))
outword = c*64
bits = 2
else
outword = outword + int(c/16)
strBase64 = strBase64 & chr(clng("&H" & hex(outword mod 256)))
outword = c*16
bits = 4
end if
end if
next
fBase64Decode = strBase64
end function
'---------------------------------------------------
function fGenerateBase64Table()
r64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
'set up decode table
dim table(256)
for x = 1 to 256 step 1
table(x) = -1
next
for x = 1 to 64 step 1
table(1+asc(mid(r64,x,1))) = x - 1
next
fGenerateBase64Table = table
end function
'---------------------------------------------------
function fSelectFile()
fSelectFile = ""
strMSHTA = "mshta.exe ""about:<input type=file id=FILE>" & _
"<"&"script>FILE.click();new ActiveXObject('Scripting.FileSystemObject')" & _
".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);<"&"/script>"""
Set wshShell = CreateObject( "WScript.Shell" )
Set objExec = wshShell.Exec( strMSHTA )
fSelectFile = objExec.StdOut.ReadLine( )
Set objExec = Nothing
Set wshShell = Nothing
end function
'-------------------------------------------------------------------------
sub getBase64()
'this can be BMP, PNG, ICO
REM sImgFile = "favicon.ico"
sImgFile = fSelectFile()
if sImgFile = "" then exit sub
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(sImgFile)
filesize = f.size
set f = fso.opentextfile(sImgFile,1,0) 'open as ascii
strBinFile = f.read(filesize)
f.close
set fso = nothing
strPNGFile = fBase64Encode(strBinFile)
s = s & "Base64 encoding of "&sImgFile&"<br><br>" & strPNGFile & "<br><br>"
s = s & "<img src=""data:image/bmp;base64," & strPNGFile & """><br><br>" & vbcrlf
imgbase64.innerhtml = s
end sub
'-------------------------------------------------------------------------
</script>
<style type="text/css">
body {font-family:"CONSOLAS";font-size:"10pt";}
input {font-family:"CONSOLAS";font-size:"8pt";}
</style>
<body>
<input type=button value="Encode an image file..."
data-tooltip title="Choose a PNG, BMP, ICO file to encode in base64"
onclick=getBase64>
<br><br>
<div id=imgbase64 style="word-wrap: break-word;"></div>
</body>
</html>