可以.BMP文件存储在HTA(HTML/VBScript)中



我在FireFox书签的备份中注意到,每个条目左侧显示的图标在a标记中作为字符流。例如:

ICON="data:image/png;base64,iVBOR[删除数据以缩短示例]rkJggg==">

我有3个BMP文件(2 are 4x20 (249 bytes) and 1 is 102x82 (24.7 KB)),我想隐藏在我的HTML应用程序中,这样它们就不会丢失。

较大的一个在样式标签中出现3次,如下所示(显示1次(:

<style type="text/css">
#frmMainBody
  {background:grey; background-image:url('Background.bmp'); margin:0;
   padding:0; font:normal 10pt Microsoft Sans Serif;}
</style>

另外两个出现在VBScript子例程中,如下所示:

Sub Button_Glow
  ' Highlights a button when the cursor hovers over it.
  With Window.Event.srcElement.Style
    If .BackgroundColor <> "Lavender" Then
      .BackgroundColor = "Lavender"
      .BackgroundImage = "url(Glow.bmp)"
      .BackgroundPositionY = -2
      .BackgroundRepeat = "Repeat-X"
    End If
  End With
End Sub 'Button_Glow

这可能吗?

HTA是一种纯文本可编辑标记语言,您可以使用任何纯文本编辑器(如Notepad++(打开和编辑它。

您可以CAN通过将图像转换为base64,然后代替,将任何图像格式存储在HTML、CSS等中

<img src="mypath/myimage.bmp" />
<style type="text/css">
foo { background: url(mypath/myimage.bmp); }
</style>

你会放::

<img src="data:image/x-png;base64,iVBORw0KGgoAAAANS....." />
<style type="text/css">
foo { background: url(data:image/x-png;base64,iVBORw0KGgoAAAANS.....); }
</style>

为了让这对你来说更容易,你可以使用在线工具将图像转换成这种格式,比如这里的工具>>将任何图像转换成Base64字符串<lt。

如何将其应用于问题中的代码

使用其中一个工具(或编写自己的工具(,定位并将"Background.bmp"转换为base64,然后像这样修改您发布的第一块代码(也缩短以节省空间(

<style type="text/css">
#frmMainBody
  {background:grey; background-image:url(data:image/x-png;base64,iVBORw0KGgoAAAANS....); margin:0;
   padding:0; font:normal 10pt Microsoft Sans Serif;}
</style>

接下来,对于VBScript代码,定位并转换"Glow.bmp"(与上面的"Background.bmp"相同(,然后修改代码块,使其看起来像这个

Sub Button_Glow
  ' Highlights a button when the cursor hovers over it.
  With Window.Event.srcElement.Style
    If .BackgroundColor <> "Lavender" Then
      .BackgroundColor = "Lavender"
      .BackgroundImage = "data:image/x-png;base64,iVBORw0KGgoAAAANS....."
      .BackgroundPositionY = -2
      .BackgroundRepeat = "Repeat-X"
    End If
  End With
End Sub

左侧的图像被称为favicon,其默认大小为16x16并且应该在中。ICO格式(但也可以是其他格式(。

当你写关于隐藏一个。BMP图像,我想你想存储而不是隐藏收藏夹图标,所以它总是加载的,即使书签/用户离线。正确的

我不擅长vb,但你可以将文件或图像编码到base64(在带有base64_encode((函数的PHP中(,vb中应该有一个函数。您所要做的就是将图像文件(例如XXXXXX.png(中的内容或字符串保存为变量,并将其传递到base64编码函数中。

你也可以使用这种方法将图像直接保存到数据库中(我想…(,但你必须解码字符串才能使图像出现。(假设你在做这个。(-不要在家里尝试。它可能会杀死你的猫并烧毁你的房子:/

这可能会有所帮助-一个使用VBScript将图像文件编码为base64的HTA(代码改编自VBScript和VBScript中的base64编码字符串以打开对话框选择文件路径(。

您可以生成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(n3)
    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(i3) = carr(a4) & carr((a And 3) * 16 + b16) & carr((b And 15) * 4 + c64) & 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>

最新更新