13

我注意到在我的 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

这可能吗 ?

4

4 回答 4

1

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

您可以通过将图像转换为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 字符串<< 。

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

使用其中一个工具(或编写您自己的工具),找到并将“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
于 2014-04-08T00:57:00.513 回答
0

左侧的图像称为favicon,默认大小为16x16,应为.ICO格式(但也可以是其他格式)。

当您撰写有关隐藏 .BMP 图像的文章时,我猜您想存储而不是隐藏图标,因此即使书签/用户处于脱机状态,它也会始终加载。正确的?

于 2013-08-18T21:01:50.323 回答
0

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

您也可以使用此方法将图像直接保存到数据库中(我猜...),但您必须对字符串进行解码才能使图像出现。(假设您正在这样做。) - 不要在家。它可能会杀死你的猫并烧毁你的房子:/

于 2013-11-12T11:13:33.753 回答
0

这可能会有所帮助 - 使用 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>
于 2017-01-11T04:17:46.793 回答