AutoHotkey Tip of the Week: Embed Images Directly in the AHK Script

Rather than Use the FileInstall AutoHotkey Command, This Trick Allows You to Lodge Images Directly into Your AutoHotkey Scripts

Light Bulb!In the past, whenever I wanted to add a graphic image to an AutoHotkey script, I needed to either separately provided the file or embed it in the compiled EXE file using the FileInstall command. The AutoHotkey world has opened my eyes to an ancient technique for embedding an image directly in the AHK script—no need to supply the file separately or embed it by compiling the script.

After I posted the MousePrecise.ahk script at the AutoHotkey Scripts and Functions Forum, I received a recommendation from @guest3456. The more experienced AutoHotkey user suggested that I embed the System Tray icon image Mouse2.ico (Mouse2directly into the script. I had no idea that you could do that! Previously, I had always used the FileInstall command to add files to compiled EXE apps, but the image file needs to accompany the AHK script file. Thanks to this suggestion, the MousePrecise.ahk script no longer depends on the availability of the external ICO graphic file. The technique uses the Image2Include.ahk script to create the function Create_Mouse2_ico() which I can include in any script.


(I discuss how the MousePrecise.ahk script works in the book AutoHotkey Hotkeys: Tips, Tricks, Techniques, and Best Practices for Automating Your Windows Computers.)

I don’t know how every piece of the Image2Include.ahk function-writing script works, but I understand it well enough to show you how to use it.

Note: The basic script (which I have made no effort to completely comprehend line-by-line) uses a series of DLL calls. The uber-AutoHotkey user, who goes by the forum name just me, based the Image2Include.ahk script on the function found in the post How to convert Image data (JPEG/PNG/GIF) to hBITMAP?” by fellow uber-AutoHotkey user SKAN.

Deeper Note: The writer of the Image2Include.ahk script, just me, considers this technique somewhat outdated—at least when compiling images with FileInstall. (The linked post is in German so you may need to click the translate button.) However, for running plain-vanilla AHK scripts (uncompiled text), it may continue as the best option for embedding images—at least until someone shows me otherwise.

I’ve posted my copy of Image2Include.ahk for download.

How to Embed an Image in an AutoHotkey Script Using Image2Include.ahk

Once you download Image2Include.ahk (or copy it from the Archived AutoHotkey Forum), adding an image to your script takes three simple steps;

  1. After loading the Image2Include.ahk script, select and convert the image file into an AutoHotkey function.
  2. Open the newly created AHK script and copy the function for insertion into your main AHK file.
  3. Modify any AutoHotkey command(s) calling the image to access the function.

Although the function code looks pretty enigmatic, you’ll find it easy to implement.

1. Select File for Conversion

When you load the Image2Include.ahk script the following GUI window activates:


File conversion intuitively uses a file selection dialog to pick the target image. Once you’ve found and added the desired filename, click “Convert Image” to create the AutoHotkey function script. The name of the AutoHotkey script appears in the status bar at the bottom of the window.

This image shows the extensions for the graphic file types processed by the Image2Include.ahk script.

The image on the right shows the supported file extensions.

You should use compressed graphic file formats (e.g. jpg, png) whenever possible. This helps to stop script bloating. For example, I clipped a region of the screen and saved it as a TIFF file. When converted to the Image2Include function, it consumed over one megabyte. Then I saved the same image as a PNG file. The created function needed less than 60 kilobytes of space. Compressed files save space.

Library Benefits

2. Insert New Function into AHK Script

You could use the #Include directive to insert the new function into your script, but, unless you plan to compile the script, that defeats the purposes of the function—eliminating extra external files.

  1. Open the new script (in this case, Create_Mouse2_ico.ahk) with any text editor.
  2. Copy the entire contents of the file (CTRL+A, then CTRL+C).
  3. Paste the function into the main AutoHotkey script (CTRL+V)—preferably toward the end of the file.

Whenever the script calls the function in the manner shown in the next section, it returns the image.

3. Modify Calling Commands

Ultimately, in AutoHotkey version 1.1, you most likely will need to use the forced expression operator % (discussed in the last blog) to implement this embedded image technique. If you only need to use the image in one AutoHotkey command, then call the function directly in the AutoHotkey command as follows:

Menu, tray, Icon, % "HBITMAP:*" . Create_Mouse2_ico()

Substitute the name of your newly created function for Create_Mouse2_ico().

In certain commands (possibly for particular types of image files), you can first save the image to a variable for evaluation as an expression in multiple commands:

Cow := "HBITMAP:*" . Create_cowskating_jpg()

SplashImage, % (toggle := !toggle) ? Cow 
  : "Dancing Dogs.jpg", cwYellow ctBlue
  , C IS FOR COW!, Is the cow laughing?
  , Laughing Cow , % (font := !font) 
  ? "Arial" : "Comic Sans MS"

(I discuss this image toggling routine in the book AutoHotkey Hotkeys: Tips, Tricks, Techniques, and Best Practices for Automating Your Windows Computers.)

I couldn’t get this preload-into-variable technique working for the Menu, tray, Icon command above—possibly due to special requirements for the command.

When to Embedded Images with Image2Include.ahk

Embedding the System Tray icon for the MousePrecise.ahk script is a no-brainer. The image is small and only used once. However, if you have numerous images to include, then providing them in a separate image folder or including them using the FileInstall command for compiling may present a better option. It’s your call.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)


This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)


One thought on “AutoHotkey Tip of the Week: Embed Images Directly in the AHK Script

  1. You shouldn’t need to force expression, the Menu command should be the same as the SplashImage. Both of these work for me:

    ico := “HBITMAP:*” . Create_Mouse2_ico()
    Menu, tray, Icon, %ico%

    ico := Create_Mouse2_ico()
    Menu, tray, Icon, HBITMAP:*%ico%

    I prefer the 2nd but its trivial really


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s