Can I embed an icon to a .hta file? - hta

I have written an HTML Application (hta file) and am wondering if there is a way to embed an icon file into the hta file itself.
I have seen html emails that include embedded graphic files, is there any way to do this with html applications and icons?
HTA files have an HTA:APPLICATION tag that allows you to specify an icon, but I want to have only a single file for download. I don't want to have an external icon file. Is this possible?
More info on hta files here: HTA files.

I've found an hack to set the icon.
Prepare an icon file icon.ico and an hta file source.hta with the following contents:
<HTML>
<HEAD>
<SCRIPT>
path = document.URL;
document.write(
'<HTA:APPLICATION ID="oHTA" APPLICATIONNAME="myApp" ICON="'+path+'">');
</SCRIPT>
</HEAD>
<BODY SCROLL="no">
Hello, World!
</BODY>
</HTML>
Open a command prompt and type:
copy /b icon.ico+source.hta iconapp.hta
That will concatenate the icon and hta into a single file.
In my test case Internet explorer skipped over the icon data and display the HTML correctly.
The path of the icon is then set to that of the .hta file itself using javascript and the icon is loaded.
I have tested this on Windows XP SP3, Internet explorer 8.

Consider using one of the applications in System32 for your icon.
This won't change the icon for an HTA file that's on the desktop, but once it's running it will add some personality to the task bar and such.
I often use the following to add a bit of charm to my HTAs.
<hta:application icon="magnify.exe" />
I don't believe it's possible to use icons from a library, but just open System32 in Explorer and change it to icon view and see if there are any icons that catch your fancy at all.

I remember seeing this a looong time ago:
<img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub//ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcppV0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7"width="16" height="14" alt="embedded folder icon">
I've never tried it myself though.

I know it's not exactly what the OP requested, but, instead of embedding an icon in the .hta, have you consider a URL to an icon file? Many websites have a website icon favicon.ico which works in HTA applications:
<HTML>
<HEAD>
<HTA:APPLICATION
ID="oHTA"
APPLICATIONNAME="myApp"
ICON="https://stackoverflow.com/favicon.ico">
</HEAD>
<BODY SCROLL="no">
Hello, World!
</BODY>
</HTML>
This gives you limitless choices in icons without the need to deploy one with your HTA application.

Quite possibly ... there is a way to embed images directly into an html file that may work for this http://www.sveinbjorn.org/news/2005-11-28-02-39-23

Another possible solution, but not completely compliant with the exact phrasing of the question, would be to convert the HTA into an .EXE file using the "HTAedit" application (http://www.htaedit.com/).
They offer a trial version that is compltely functional (just some startup nag screen and no history/recent files) without blocking creation of .EXE from the HTA source, with no time limit.
You can then declare your icon with the ICON="myicon.ico" statement in the HTA header block, then when it "compiles" (according to my opinion it's more likely rather a packager than a real compiler but that's not the point here) it ask you for additional resource files. If your icon file is not already listed there, then just add it into the list, "et voilà !". You get a nice executable with it's version number and embedded icon.
Actually that's what I did with the Microsoft Scripting Guys tools (HTA_HELPOMATIC.HTA and SCRIPTOMATIC.HTA): I just changed them in EXE adding an icon from some %windir%\system32 exe/dll I found matching more or less the meaning of the tools, and it worked perfectly.
On the run it might be more portable than just embedding the icon into the HTA (HTAedit tool seems to be able to produce W7/64 executables but I did not test it that way, I'm still under XP32-SP2...) and it keeps your source hta file readable and editable by a text editor.

IE doesn't support data URIs, so you're going to have to use an external file if you use the img tag.
The only thing I can think of is to use VML, which has been around since IE5. It's an SVG-like vector image format that can be used inline. For example, draw something using this VML editor and click "Get code". You can plop that in your HTA. I'm not aware of anything that will convert your image to VML directly, but I believe there is a way to export to VML from some Office products.

It's a pretty far-fetched answer, but you could embed the icon as base64-encoded XML in the HTA, then use JavaScript onload and save the icon file to a temporary location. The ActiveX Object MSXML.DomDocument can encode and decode base64 nodes.

As soon as you need an <iframe> or other HTML dialog you're going to want additional files anyway. You'll generaly find that .CSS and .VBS files separate from the .HTA make programming and support a lot easier for any non-trivial HTA too.
One alternative for doing this as "a single EXE" is to wrap everything up as a self-extracting archive or via IExpress. When the user "runs your program" it extracts everything from your archive into a temp directory and runs the item of your choice.
There are 3rd party alternatives like HTMLApp too.

Another solution, but not completely compliant with the exact phrasing of the question, is to create a simple shortcut. For 64-bit systems you should enter:
target: C:\Windows\SysWOW64\mshta.exe C:\path+filename.hta
start in: C:\Windows\SysWOW64
You can manually change the icon of the shortcut.
The user can drag the shortcut to the taskbar.
The extention of the hta file is of no importance to mshta.exe. So if you give it a custom extension, then the hta files will show with the corresponding custom icon.

You could embed a base64 encoded image into the HTA and create the file locally on the first execution.
Below is a HTA that creates a "favicon.ico" (https://stackoverflow.com/favicon.ico) file from a base64 string. It can encode an image file also (with code adapted from Base64 Encode String in VBScript and VBScript to open a dialog to select a filepath).
<!DOCTYPE html>
<html>
<head>
<HTA:APPLICATION
ID="oHta"
APPLICATIONNAME="Icon test..."
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>Icon test</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
'-------------------------------------------------------------------------
sub setup()
'https://stackoverflow.com/favicon.ico in base64
base64Icon="AAABAAIAEBAAAAEAIABoBAAAJgAAACAgAAABACAAqBAAAI4EAAAoAAAAEAAAACAAAAABACAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAqaOe/6mjnv+po57/qaOe/6mjnv+po57/qaOe/6mjnv+po57/qaOe/6mjnv8AAAAAAAAAAAAAAAAAAAAAAAAAAKmjnv8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACpo57/AAAAAAAAAAAAAAAAAAAAAAAAAACpo57/AAAAAAlw8v8JcPL/CXDy/wlw8v8JcPL/CXDy/wlw8v8AAAAAqaOe/wAAAAAAAAAAAAAAAAAAAAAAAAAAqaOe/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlw8hMJcPI2AAAAAKmjnv8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACXDyLwlw8l0JcPKJCXDytglw8uIJcPLvCXDyvQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlw8sIJcPKlCXDydwlw8kkJcPIdCXDyEwlw8nEJcPIvAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJcPI9CXDypQlw8u8JcPKgCXDyLwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACXDyDwlw8nEJcPLWCXDy0wlw8msJcPIPCXDyPQlw8uIJcPInAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlw8iMJcPKgCXDyOgAAAAAAAAAACXDydwlw8ugJcPJGCXDyUQlw8oIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJcPITCXDytglw8sIJcPIdCXDyGAlw8ugJcPI2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJcPI6CXDy4glw8okJcPIDAAAAAAlw8rYJcPJ+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACXDyZAlw8kkAAAAAAAAAAAlw8msJcPLICXDyAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlw8icJcPLoCXDyIwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJcPLCCXDyZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACXDyHQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//AADABwAA3/cAANAXAADflwAA8B8AAPAPAAD+DwAA8AcAAPGDAAD+AwAA/CcAAPzHAAD/jwAA/58AAP+/AAAoAAAAIAAAAEAAAAABACAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAA////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AKmjngCpo54AqaOeAKmjngCpo54AqaOeAKmjngCpo54AqaOeAKmjngCpo54AqaOeAKmjngCpo54AqaOeAKmjngCpo54AqaOeAKmjngCpo54AqaOeAKmjngD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8AqaOeAKmjngCpo54AqaOeAKmjngCpo54AqaOeAKmjngCpo54AqaOeAKmjngCpo54AqaOeAKmjngCpo54AqaOeAKmjngCpo54AqaOeAKmjngCpo54AqaOeAP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wCpo54AqaOe/6mjnv+po57/qaOe/6mjnv+po57/qaOe/6mjnv+po57/qaOe/6mjnv+po57/qaOe/6mjnv+po57/qaOe/6mjnv+po57/qaOe/6mjnv+po54A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AKmjngCpo57/qaOe/6mjnv+po57/qaOe/6mjnv+po57/qaOe/6mjnv+po57/qaOe/6mjnv+po57/qaOe/6mjnv+po57/qaOe/6mjnv+po57/qaOe/6mjngD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8AqaOeAKmjnv+po57/JID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0AKmjnv+po57/JID0ACSA9AAkgPQAJID0AP///wD///8A////AP///wD///8A////AP///wCpo54AqaOe/6mjnv8kgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAqaOe/6mjnv8kgPQAJID0ACSA9AAkgPQA////AP///wD///8A////AP///wD///8A////AKmjngCpo57/qaOe/ySA9AAkgPQAJID0/ySA9P8kgPT/JID0/ySA9P8kgPT/JID0/ySA9P8kgPT/JID0/ySA9P8kgPT/JID0ACSA9ACpo57/qaOe/ySA9AAkgPQAJID0ACSA9AD///8A////AP///wD///8A////AP///wD///8AqaOeAKmjnv+po57/JID0ACSA9AAkgPT/JID0/ySA9P8kgPT/JID0/ySA9P8kgPT/JID0/ySA9P8kgPT/JID0/ySA9P8kgPQAJID0AKmjnv+po57/JID0ACSA9AAkgPQAJID0AP///wD///8A////AP///wD///8A////AP///wCpo54AqaOe/6mjnv8kgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAqaOe/6mjnv8kgPQAJID0ACSA9AAkgPQA////AP///wD///8A////AP///wD///8A////AKmjngCpo57/qaOe/ySA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9B4kgPRRJID0gSSA9LQkgPTjJID0EiSA9ACpo57/qaOe/ySA9AAkgPQAJID0ACSA9AD///8A////AP///wD///8A////AP///wD///8AqaOeAKmjngCpo54AJID0ACSA9AAkgPQAJID0AiSA9CYkgPRXJID0iSSA9LokgPTtJID0/ySA9P8kgPT/JID0/ySA9P8kgPRKJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wAkgPQAJID0ACSA9LgkgPTxJID0/ySA9P8kgPT/JID0/ySA9P8kgPT/JID0+SSA9M0kgPSaJID0aiSA9CQkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQA////AP///wD///8A////AP///wD///8A////AP///wD///8A////ACSA9AAkgPQAJID0vSSA9P8kgPT/JID09CSA9MUkgPSUJID0YiSA9DEkgPQFJID0ACSA9DQkgPSjJID05iSA9A0kgPQAJID0ACSA9AAkgPQAJID0ACSA9AD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8AJID0ACSA9AAkgPRKJID0WySA9CkkgPQDJID0ACSA9AAkgPQAJID0BSSA9FgkgPTHJID0/ySA9P8kgPT/JID0eCSA9AAkgPQAJID0ACSA9AAkgPQAJID0AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0FSSA9HwkgPTkJID0/ySA9P8kgPT/JID02SSA9GwkgPQ9JID0LCSA9AAkgPQAJID0ACSA9AAkgPQA////AP///wD///8A////AP///wD///8A////AP///wD///8A////ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0MiSA9J8kgPT4JID0/ySA9P8kgPT+JID0tySA9EkkgPQCJID0YiSA9PckgPTjJID0HCSA9AAkgPQAJID0ACSA9AD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8AJID0ACSA9AAkgPQAJID0KiSA9MIkgPT/JID0/ySA9P8kgPTyJID0lCSA9CYkgPQAJID0CCSA9J8kgPT/JID0/ySA9OkkgPRGJID0IySA9AAkgPQAJID0AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wAkgPQAJID0ACSA9AAkgPQPJID07iSA9P8kgPTcJID0cCSA9A8kgPQAJID0ACSA9CQkgPTPJID0/ySA9P8kgPTDJID0HCSA9K4kgPTzJID0ZiSA9AAkgPQA////AP///wD///8A////AP///wD///8A////AP///wD///8A////ACSA9AAkgPQAJID0ACSA9AAkgPRaJID0TCSA9AIkgPQAJID0ACSA9AAkgPRQJID07ySA9P8kgPT+JID0jiSA9AUkgPR+JID0/ySA9P8kgPSOJID0ACSA9AD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8AJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQEJID0iCSA9P4kgPT/JID08SSA9FQkgPQAJID0TSSA9P4kgPT/JID0uySA9AMkgPQAJID0AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0GCSA9MAkgPT/JID0/ySA9NMkgPQmJID0ACSA9CgkgPTwJID0/ySA9N4kgPQUJID0ACSA9AAkgPQA////AP///wD///8A////AP///wD///8A////AP///wD///8A////ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9B8kgPTnJID0/ySA9P8kgPSiJID0CiSA9AAkgPQPJID02CSA9P8kgPT1JID0LiSA9AAkgPQAJID0ACSA9AD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8AJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9JYkgPT4JID0ZySA9AAkgPQAJID0AiSA9LQkgPT/JID0/iSA9FYkgPQAJID0ACSA9AAkgPQAJID0AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0AySA9CokgPQAJID0ACSA9AAkgPSCJID0/ySA9P8kgPSIJID0ACSA9AAkgPQAJID0ACSA9AAkgPQA////AP///wD///8A////AP///wD///8A////AP///wD///8A////ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0UySA9P4kgPT/JID0tySA9AMkgPQAJID0ACSA9AAkgPQAJID0ACSA9AD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8AJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9A8kgPTvJID0/ySA9NskgPQQJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9DQkgPTIJID0LCSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQA////AP///wD///8A////AP///wD///8A////AP///wD///8A////ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8AJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0ACSA9AAkgPQAJID0AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP////////////////wAAD/8AAA//P//P/z//z/8wAM//MADP/z//z/8/4E//+AB///AAf//wBD//8OA///+AH//+AA//+AQH//gYA//8cAP//8CD//+BB///Ag///4wf//+cP///+D////B////4///////////////////////"
Set fso = CreateObject("Scripting.FileSystemObject")
if not fso.fileexists("favicon.ico") then
strBin = fBase64Decode(base64Icon)
set outfile = fso.createtextfile("favicon.ico")
outfile.write strBin
outfile.close
document.title = "Need to refresh to see the new icon"
end if
set fso = nothing
end sub
</script>
<style type="text/css">
body {font-family:"CONSOLAS";font-size:"10pt";}
input {font-family:"CONSOLAS";font-size:"8pt";}
</style>
<body onLoad=setup()>
<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>

Related

Classic ASP which folder does a file belong to

I have a folder structure which stores images by the thousand, take for example the following folder names;
00001-01000
01001-02000
02001-03000
03001-04000
04001-05000
05001-06000
06001-07000
I am then dynamically rendering a page to display all sorts of images. In the page i am trying to build a link to the image, but what i need is to figure out what folder an image belongs to. For instance, i am using the following to get the image id from the database
<%= rs.Fields("imageid") %>
If that were to return '04232' the folder name that would belong to would be '04001-05000'.
Is there any way that i could figure out what the folder name would be, by only having the imageid in classic asp?
For this you need two small methods. First, padding a string:
Function PadLeft(str, padChar, desiredLength)
Dim result
result = CStr(str)
Do Until Len(result)>=desiredLength
result = padChar & result
Loop
PadLeft = result
End Function
And the method to find the folder name:
Function ExtractFolderName(imageFileName)
Dim numericValue, rangeStart, rangeEnd
numericValue = CInt(imageFileName)
rangeStart = (Fix((numericValue / 1000)) * 1000) + 1
rangeEnd = (Fix((numericValue / 1000)) + 1) * 1000
ExtractFolderName = PadLeft(rangeStart, "0", 5) & "-" & PadLeft(rangeEnd, "0", 5)
End Function
To use it:
folderName = ExtractFolderName(rs("imageid"))
Basically, the method performs some math on the name to find the desired range. Note this will throw error in case of a non numeric value in the database.

How to bind client key for particular client class in Awesome WM?

I want to bind client key to perform some operation on Mod1-q shortcut only in vivaldi browser, so I write in clientkeys config in my rc.lua:
awful.key({"Mod1"}, "q",
function (c)
if c.class == "Vivaldi-stable" then
someoperation()
end
end,
{description = "Some operation only in browser", group = "client"})
The problem with this code is that someoperation() is performed if Mod1+q is pressed in vivaldi browser, but all other window got no input at all, while I want them to receive Mod1+q as before. Is there some obvious way to bind keys only to specific client class, maybe via match function?
Add the following to a suitable entry (with class = "Vivaldi[-]stable") to awful.rules:
callback = function(c)
local keys = c:keys()
c:keys(gears.table.join(keys,
awful.key({"Mod1"}, "q", someoperation,
{ description = "Some operation only in browser", group = "client"})))
end

Html Table export to PDF with iTextSharp Library Chinese Character not show [duplicate]

I'm facing a problem when trying to export a Vietnamese document as PDF using iText.
I put Vietnamese words in .xml file like this
<td fontfamily="Helvetica" fontstyle="0" fontsize="9" align="0" colspan="48" lineoccupied="1">T\u1ED5 ch\u1EE9c tham gia</td>
then having java to get the phrases from xml file and convert it into Unicode using this method:
public String convertToUnicode(String s) {
int i = 0, len = s.length();
char c;
StringBuffer sb = new StringBuffer(len);
try {
while (i < len) {
c = s.charAt(i++);
if (c == '\\') {
if (i < len) {
c = s.charAt(i++);
if (c == 'u') {
if (Character.digit(s.charAt(i), 16) != -1
&& Character.digit(s.charAt(i + 1), 16) != -1
&& Character.digit(s.charAt(i + 2), 16) != -1
&& Character.digit(s.charAt(i + 3), 16) != -1) {
if (s.substring(i).length() >= 4) {
c = (char) Integer.parseInt(s.substring(i, i + 4), 16);
i += 4;
} else {
sb.append('\\');
}
} else {
sb.append('\\');
}
} // add other cases here as desired...
}
} // fall through: \ escapes itself, quotes any character but u
sb.append(c);
}
} catch (Exception e) {
System.out.println("Error Generate PDF :: " + e.getStackTrace().toString());
return s;
}
return sb.toString();
}
After that, export String to PDF - encoding UTF-8.
But the program failed to display Vietnamese character '\u1ED5' and '\u1EE9'
The output becomes "T chc tham gia"
Could you please show me how to fix this issue?
Thanks :)
There are 3 XML Worker examples involving Asian languages on the official iText web site. They parse an XHTML file containing Chinese characters, but it should be easy to adapt them to Vietnamese examples.
You can find the HTML files were going to parse here:
hero.html
hero2.html
Both files contain the following text:
長空 (Broken Sword), 秦王殘劍 (Flying Snow), 飛雪 (Moon), 如月 (the King), and 秦王 (Sky).
In the first case, a font is defined using CSS:
<span style="font-size:12.0pt; font-family:MS Mincho">長空</span>
In the second case, no specific font is defined:
<body><p>長空 (Broken Sword), 秦王殘劍 (Flying Snow), 飛雪 (Moon), 如月 (the King), and 秦王 (Sky).</p></body>
These files contain UTF-8 characters, so we're going to parse them like this:
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new FileInputStream(HTML), Charset.forName("UTF-8"));
The first thing you need, is a font that supports Vietnamese characters. That's something iText can't help you with. In your HTML file, you've defined Helvetica, but that's a standard Type1 font that is never embedded when using iText and that doesn't know how to draw Vietnamese glyphs. That's never going to work.
The first example D07_ParseHtmlAsian will automatically search for a font named MS Mincho. If it finds that font (for instance because you have msmincho.ttc in your Windows fonts directory), the font will show up in your PDF. See hero.pdf. If it doesn't find a font with that name, then the glyphs won't be visible, because you didn't provide any font program for those glyphs.
The second example D07bis_ParseHtmlAsian offers a workaround in case you don't have MS Mincho anywhere. In that case, you have to use an XMLWorkerFontProvider and register a font that can be used instead of MS Mincho. For instance: we use a font stored in the file cfmingeb.ttf and assign the alias MS Mincho:
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register("resources/fonts/cfmingeb.ttf", "MS Mincho");
The resulting file asian.pdf is slightly different from what we expect, but now we can at least see the Chinese glyphs.
In the third example, the HTML file doesn't tell us anything about the font that needs to be used. We'll define the font using CSS like this:
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream("body {font-family:tsc fming s tt}".getBytes()));
cssResolver.addCss(cssFile);
Now, all the text in the body will use the font TSC FMing S TT (stored in the file cfmingeb.ttf). You can see the difference in the resulting PDF asian2.pdf.
I think you need an encoding as UTF-8 for your HTML and use &#xUNUM; for hex or &#NUM; for regular code to embed your special characters. Not sure where but somewhere in your program since it is not display shown, but your final HTML should be:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML LEVEL 1//EN">
<HTML>
<HEAD>
<TITLE>Your Page Title</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</HEAD>
<BODY>
<!-- YOUR CONTENT HERE -->
<td fontfamily="Helvetica" fontstyle="0" fontsize="9"
align="0" colspan="48"
lineoccupied="1">Tổ chức tham gia</td>
</BODY>
</HTML>
You can cut and paste the above into an HTML file and view the result. More reading pleasure is here Unicode and HTML

QRCode.net (from CodePlex) always replaces first character with an asterisk?

Using QRCode.net assembly to generate QRCode images for URLs, I've tried most of the properties / options available and the URLs first character is always replaced with an asterisk (*). As the QRCode text is to be a URL, this means that some QRCode reader apps don't recognise my QRCode as a valid URL. For now, I pad the URL with a ' ' space so you at least get '*http://myapp.com', instead of '*ttp://myapp.com', but this always happens..
QRCode.Net on CodePlex
Has anyone seen this before, or know which setting stops it, I've tried most, here's my code:
Dim iee As New ImageEncoder()
iee.AutoConfigurate = True
iee.ECI = -1
iee.Encoding = 4
iee.Fnc1Mode = 0
iee.ErrorCorrectionLevel = 0
iee.Version = 1
iee.ProcessTilde = False
iee.MarginSize = 10
iee.ModuleSize = 6
iee.StructuredAppend = False
iee.StructuredAppendCounter = 0
iee.StructuredAppendIndex = 0
iee.TextData = " http://myApp.com/?querystringdata=here")
Dim image As Bitmap = iee.Encode2Image()
Dim tempSitePath As String
tempSitePath = HttpContext.Current.Server.MapPath(HttpContext.Current.Session("dirTempPath"))
image.Save(tempSitePath + "loc_" + locid + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
This was bugged with them.. Ended up using a different library..
I found a demo library for QRCode.net.
http://www.codeproject.com/Articles/258779/Just-launched-new-open-source-project-QrCode-Net-a
This has downloadable source-code. I opened the solution, made a few changes to get the project to build and it created codes with no asterisk (*). I believe the demo code is older than this post, but perhaps the library has also been updated with a fix.

Need to pull out a link from a website using VBscript

I have got as far as getting the HTML response into a variable and now I am stuck:
Link: http://www.avg.com/gb-en/31.prd-avb
In an ideal world I would be able to get the first and second link for x86 and x64. All I need to get is the actual location of the exe into a variable: IE:
download.avg.com/filedir/inst/avg_ipw_x86_all_2011_1204a3402.exe
Can anyone point me in the right direction?
Thanks in advance for any help provided
This works, but it's far from a stellar technique, because I'm parsing HTML with regex.
However, I'm not aware of an easier method to accomplish this in Classic ASP, and this is a simple task.
<%
url = "http://www.avg.com/gb-en/31.prd-avb"
Dim http
Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
http.SetTimeouts 20000, 20000, 20000, 10000
http.Open "GET", url, False
http.Send
If http.WaitForResponse(5) Then
responseText = http.ResponseText
End If
Set http = Nothing
'Response.Write(Server.HtmlEncode(responseText))
Set re = New RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "<a href=""(http://download\.avg\.com/filedir/inst/.*?)"""
Set matches = re.Execute(responseText)
If matches.Count > 0 Then
For Each match In matches
Response.Write(match.SubMatches(0) & "<br />")
Next
Else
Response.Write("No matches.")
End If
%>
Gives output like this:
http://download.avg.com/filedir/inst/avg_ipw_x86_all_2011_1204a3402.exe
http://download.avg.com/filedir/inst/avg_ipw_x64_all_2011_1204a3402.exe
http://download.avg.com/filedir/inst/avg_msw_x86_all_2011_1204a3402.exe
http://download.avg.com/filedir/inst/avg_msw_x64_all_2011_1204a3402.exe
http://download.avg.com/filedir/inst/avg_rad_x86_all_2011_1154.exe
http://download.avg.com/filedir/inst/avg_rad_x64_all_2011_1154.exe

Resources