【问题标题】:Can a .BMP file be stored in an HTA (HTML/VBScript)?.BMP 文件可以存储在 HTA (HTML/VBScript) 中吗?
【发布时间】:2013-05-08 20:37:54
【问题描述】:

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

这可能吗?

【问题讨论】:

  • 是的,但我会先转换为 PNG。无论哪种情况,这都会将图像转换为 CSS/HTML 的数据 uri; webcodertools.com/imagetobase64converter
  • Alex,我需要对我的代码进行哪些更改?
  • 使用那个在线工具;它将生成一个大的url(data:XXX) 字符串,只需用它替换url('Background.bmp')
  • 谢谢亚历克斯。我试试看。
  • 您的图像格式不会有太大帮助。 Base64 使用每字节 6 位来编码数据,而二进制使用每字节 8 位,这还不包括标头的 814 字节开销。查看en.wikipedia.org/wiki/Base64#MIME 了解一些有趣的细节。

标签: html vbscript base64 bmp hta


【解决方案1】:

左边的图片名为favicon,默认尺寸为16x16,应该是.ICO格式(但它也可以在其他)。

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      可以将任何图像格式存储为 HTML、CSS 等,方法是将图像转换为 base64,然后代替

      <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>
      

      为了让您更轻松,您可以使用在线工具(例如此处的工具)将图像转换为这种格式 >> Convert any Image into a Base64 string

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

      使用其中一个工具(或自己编写),找到“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
      

      【讨论】:

      【解决方案4】:

      这可能会有所帮助 - 一个使用 VBScript 将图像文件编码为 base64 的 HTA(代码改编自 Base64 Encode String in VBScriptVBScript to open a dialog to select a filepath)。

      您可以生成base64代码并将其用作图像的来源,例如:

      &lt;img src="data:image/png;base64, [base64 code inserted here] "&gt;

      <!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>
      

      【讨论】:

        猜你喜欢
        • 2013-11-13
        • 1970-01-01
        • 2014-12-25
        • 2012-01-22
        • 1970-01-01
        • 1970-01-01
        • 2010-09-15
        • 2011-12-25
        • 2011-11-04
        相关资源
        最近更新 更多