Compiling images into exe?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
dice4321
Posts: 8
Joined: 21 May 2016, 21:44

Compiling images into exe?

21 May 2016, 21:51

I have this code and I want to share it with my friend, the only problem is when I compile the code, it doesn't compile the images as well, I heard FileInstall would help, but I have no idea how to use it, if someone could show me an example of say, how FileInstall would be used on

Code: Select all

  ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 G:\KVC\New folder\Captures\capture(1).png 
that would be great, or even better, if someone would show me how to compile the images into the .exe without the having .exe install the images onto my friends computer!

Code: Select all

; This script was created using Pulover's Macro Creator
; www.macrocreator.com

#NoEnv
SetWorkingDir %A_ScriptDir%
CoordMode, Mouse, Window
SendMode Input
#SingleInstance Force
SetTitleMatchMode 2
#WinActivateForce
SetControlDelay 1
SetWinDelay 0
SetKeyDelay -1
SetMouseDelay -1
SetBatchLines -1


F3::
Macro1:
WinActivate, KC3改 - Google Chrome ahk_class Chrome_WidgetWin_1
Sleep, 333
Loop
{
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 G:\KVC\New folder\Captures\capture(1).png
    CenterImgSrchCoords("*95 G:\KVC\New folder\Captures\capture(1).png", FoundX, FoundY)
    If ErrorLevel = 0
    	Click, %FoundX%, %FoundY% Left, 1
    If ErrorLevel
    {
        MsgBox, 49, Continue?, Image / Pixel Not Found.`nPress OK to continue.
        IfMsgBox, Cancel
        	Return
    }
    Sleep, 1000
}
Until ErrorLevel = 0
Loop
{
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 G:\KVC\New folder\Captures\capture(2).png
    CenterImgSrchCoords("*95 G:\KVC\New folder\Captures\capture(2).png", FoundX, FoundY)
    If ErrorLevel = 0
    	Click, %FoundX%, %FoundY% Left, 1
    Sleep, 1000
}
Until ErrorLevel = 0
Loop
{
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 G:\KVC\New folder\Captures\capture(3).png
    CenterImgSrchCoords("*95 G:\KVC\New folder\Captures\capture(3).png", FoundX, FoundY)
    If ErrorLevel = 0
    	Click, %FoundX%, %FoundY% Left, 1
    Sleep, 1000
}
Until ErrorLevel = 0
Loop
{
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 G:\KVC\New folder\Captures\capture(4).png
    CenterImgSrchCoords("*95 G:\KVC\New folder\Captures\capture(4).png", FoundX, FoundY)
    If ErrorLevel = 0
    	Click, %FoundX%, %FoundY% Left, 1
    Sleep, 1000
}
Until ErrorLevel = 0
Loop
{
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 G:\KVC\New folder\Captures\capture(5).png
    CenterImgSrchCoords("*95 G:\KVC\New folder\Captures\capture(5).png", FoundX, FoundY)
    If ErrorLevel = 0
    	Click, %FoundX%, %FoundY% Left, 1
    Sleep, 1000
}
Until ErrorLevel = 0
Loop
{
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 G:\KVC\New folder\Captures\capture(6).png
    CenterImgSrchCoords("*95 G:\KVC\New folder\Captures\capture(6).png", FoundX, FoundY)
    If ErrorLevel = 0
    	Click, %FoundX%, %FoundY% Left, 1
    Sleep, 2000
}
Until ErrorLevel = 0
Sleep, 2000
Loop
{
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 G:\KVC\New folder\Captures\capture(6).png
    CenterImgSrchCoords("*95 G:\KVC\New folder\Captures\capture(6).png", FoundX, FoundY)
    If ErrorLevel = 0
    	Click, %FoundX%, %FoundY% Left, 1
    Sleep, 2000
}
Until ErrorLevel = 0
Loop, 8
{
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 G:\KVC\New folder\Captures\capture(7).png
    CenterImgSrchCoords("*95 G:\KVC\New folder\Captures\capture(7).png", FoundX, FoundY)
    If ErrorLevel = 0
    	Click, %FoundX%, %FoundY% Left, 1
    Sleep, 1000
}
Loop
{
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 G:\KVC\New folder\Captures\capture(8).png
    CenterImgSrchCoords("*95 G:\KVC\New folder\Captures\capture(8).png", FoundX, FoundY)
    If ErrorLevel = 0
    	Click, %FoundX%, %FoundY% Left, 1
    Sleep, 1000
}
Until ErrorLevel = 0
Sleep, 3000
Loop
{
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 G:\KVC\New folder\Captures\capture(9).png
    CenterImgSrchCoords("*95 G:\KVC\New folder\Captures\capture(9).png", FoundX, FoundY)
    If ErrorLevel = 0
    	Click, %FoundX%, %FoundY% Left, 1
    Sleep, 1000
}
Until ErrorLevel = 0
Sleep, 1187
Click, 518, 351 Left, Down
Sleep, 125
Click, 518, 351 Left, Up
Loop
{
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 G:\KVC\New folder\Captures\capture(12).png
    CenterImgSrchCoords("*95 G:\KVC\New folder\Captures\capture(12).png", FoundX, FoundY)
    If ErrorLevel = 0
    	Click, %FoundX%, %FoundY% Left, 1
	If ErrorLevel
    	Click, 518, 351 Left, 1
    Sleep, 1000
}
Until ErrorLevel = 0
WinActivate, KC3改 - Google Chrome ahk_class Chrome_WidgetWin_1
Sleep, 333
Sleep, 1109
Click, 253, 270 Left, Down
Sleep, 63
Click, 253, 270 Left, Up
Sleep, 953
Click, 709, 544 Left, Down
Sleep, 63
Click, 709, 544 Left, Up
Sleep, 1531
Click, 75, 169 Left, Down
Sleep, 78
Click, 75, 169 Left, Up
Return


F8::ExitApp

CenterImgSrchCoords(File, ByRef CoordX, ByRef CoordY)
{
	static LoadedPic
	LastEL := ErrorLevel
	Gui, Pict:Add, Pic, vLoadedPic, %File%
	GuiControlGet, LoadedPic, Pict:Pos
	Gui, Pict:Destroy
	CoordX += LoadedPicW // 2
	CoordY += LoadedPicH // 2
	ErrorLevel := LastEL
}
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Compiling images into exe?

22 May 2016, 07:40

Code: Select all

FileInstall, G:\KVC\New folder\Captures\capture(1).png, capture(1).png ; put in auto-execute section at top of script

; change the imagesearch commands like this:
ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 capture(1).png
Obviously do the same thing for each image
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Compiling images into exe?

22 May 2016, 09:10

how to compile the images into the .exe without the having .exe install the images
If you really need it ( i would prefer fileinstall like Sifjar showed) and ready to do some work :)

The latest version of ahk lets you use hbitmap handle instead of filename:
ImageSearch, x,y,0,0,a_screenwidth,a_screenheight,HBITMAP:*%hbitmap%

just me posted this code to convert an image to include in the code and extract the hbitmap handle.If you read this "just me" can you post the link to your post ?

Code: Select all


; ======================================================================================================================
; Function:       Creates AHK #Include files for images providing a function which will internally create a bitmap/icon.
; AHK version:    1.1.10.01 (U32)
; Script version: 1.0.00.02/2013-06-02/just me - added support for icons (HICON)
;                 1.0.00.01/2013-05-18/just me - fixed bug producing invalid function names
;                 1.0.00.00/2013-04-30/just me
; Credits:        Bitmap creation is based on "How to convert Image data (JPEG/PNG/GIF) to hBITMAP?" by SKAN ->
;                 http://www.autohotkey.com/board/topic/21213-how-to-convert-image-data-jpegpnggif-to-hbitmap/?p=139257
; ======================================================================================================================
; This software is provided 'as-is', without any express or implied warranty.
; In no event will the authors be held liable for any damages arising from the use of this software.
; ======================================================================================================================
#NoEnv
SetBatchLines, -1
; ======================================================================================================================
; Some strings used to generate the #Include file
; ======================================================================================================================
Header1 := "
(Join`r`n
; ##################################################################################
; # This #Include file was generated by Image2Include.ahk, you must not change it! #
; ##################################################################################
)"
; ----------------------------------------------------------------------------------------------------------------------
Header2 := "
(Join`r`n
If (NewHandle)
   hBitmap := 0
If (hBitmap)
   Return hBitmap
)"
; ----------------------------------------------------------------------------------------------------------------------
Footer1 := "
(Join`r`n
If !DllCall(""Crypt32.dll\CryptStringToBinary"", ""Ptr"", &B64, ""UInt"", 0, ""UInt"", 0x01, ""Ptr"", 0, ""UIntP"", DecLen, ""Ptr"", 0, ""Ptr"", 0)
   Return False
VarSetCapacity(Dec, DecLen, 0)
If !DllCall(""Crypt32.dll\CryptStringToBinary"", ""Ptr"", &B64, ""UInt"", 0, ""UInt"", 0x01, ""Ptr"", &Dec, ""UIntP"", DecLen, ""Ptr"", 0, ""Ptr"", 0)
   Return False
; Bitmap creation adopted from ""How to convert Image data (JPEG/PNG/GIF) to hBITMAP?"" by SKAN
; -> http://www.autohotkey.com/board/topic/21213-how-to-convert-image-data-jpegpnggif-to-hbitmap/?p=139257
hData := DllCall(""Kernel32.dll\GlobalAlloc"", ""UInt"", 2, ""UPtr"", DecLen, ""UPtr"")
pData := DllCall(""Kernel32.dll\GlobalLock"", ""Ptr"", hData, ""UPtr"")
DllCall(""Kernel32.dll\RtlMoveMemory"", ""Ptr"", pData, ""Ptr"", &Dec, ""UPtr"", DecLen)
DllCall(""Kernel32.dll\GlobalUnlock"", ""Ptr"", hData)
DllCall(""Ole32.dll\CreateStreamOnHGlobal"", ""Ptr"", hData, ""Int"", True, ""PtrP"", pStream)
hGdip := DllCall(""Kernel32.dll\LoadLibrary"", ""Str"", ""Gdiplus.dll"", ""UPtr"")
VarSetCapacity(SI, 16, 0), NumPut(1, SI, 0, ""UChar"")
DllCall(""Gdiplus.dll\GdiplusStartup"", ""PtrP"", pToken, ""Ptr"", &SI, ""Ptr"", 0)
DllCall(""Gdiplus.dll\GdipCreateBitmapFromStream"",  ""Ptr"", pStream, ""PtrP"", pBitmap)
)"
; ----------------------------------------------------------------------------------------------------------------------
Footer2 := "
(Join`r`n
DllCall(""Gdiplus.dll\GdipDisposeImage"", ""Ptr"", pBitmap)
DllCall(""Gdiplus.dll\GdiplusShutdown"", ""Ptr"", pToken)
DllCall(""Kernel32.dll\FreeLibrary"", ""Ptr"", hGdip)
DllCall(NumGet(NumGet(pStream + 0, 0, ""UPtr"") + (A_PtrSize * 2), 0, ""UPtr""), ""Ptr"", pStream)
Return hBitmap
}
)"
; ======================================================================================================================
; Read INI file
; ======================================================================================================================
IniFile := A_ScriptFullPath . ":INI"
ImgDir := OutDir := A_ScriptDir
IniRead, Value, %IniFile%, Image2Include, ImgDir, 0
If (Value) && InStr(FileExist(Value), "D")
   ImgDir := Value
IniRead, Value, %IniFile%, Image2Include, OutDir, 0
If (Value) && InStr(FileExist(Value), "D")
   OutDir := Value
ImgFile := ""
; ======================================================================================================================
; Select an image file
; ======================================================================================================================
Gui, Margin, 20, 20
Gui, Add, Text, xm cNavy, Select Image File:
Gui, Add, Edit, xm y+5 w400 vImgFile +ReadOnly
Gui, Add, Button, x+10 yp hp vBtnFile gSelectFile, ...
Gui, Add, Text, xm y+5 cNavy, Select Output Directory:
Gui, Add, Edit, xm y+5 w400 vOutDir +ReadOnly, %OutDir%
Gui, Add, Button, x+10 yp hp vBtnFolder gSelectFolder, ...
GuiControlGet, P1, Pos, BtnFolder
Gui, Add, Text, xm y+5 cNavy, Script File:
Gui, Add, Edit, xm y+5 w400 vOutFile +ReadOnly
Gui, Add, CheckBox, xm vCreateOnLoad, Create at load-time
Gui, Add, CheckBox, xm vReturnHICON, Return HICON handle
Gui, Add, Button, xm vP2 gConvert, Convert Image
GuiControlGet, P2, Pos
Gui, Add, Button, % "x" . (P1X + P1W - P2W) . " yp wp gShow", Show Script
Gui, Add, StatusBar
Gui, Show, , Convert Image to #Include File
GuiControl, Focus, BtnFile
; Prepare the GUI which will show the generated script on demand
Gui, Script: +Owner1
Gui, Script: Margin, 0, 0
Gui, Script: Font, , Courier New
Gui, Script: Add, Edit, x0 y0 w0 h0
Gui, Script: Add, Edit, % "x0 y0 w" . Round(A_ScreenWidth * 0.8) . " h" . Round(A_ScreenHeight * 0.8)
                      . " vScriptEdit +hwndHEdit +HScroll"
Gui, Script: Add, StatusBar
Return
; ----------------------------------------------------------------------------------------------------------------------
GuiClose:
ExitApp
; ----------------------------------------------------------------------------------------------------------------------
SelectFile:
   Gui, +OwnDialogs
   FileSelectFile, ImgFile, 1, %ImgDir%, Select a Picture
                 , Img (*.bmp; *.emf; *.exif; *.gif; *.ico; *.jpg; *.png; *.tif; *.wmf)
   If (ErrorLevel)
      Return
   GuiControl, , ImgFile, %ImgFile%
   SplitPath, ImgFile, , ImgDir, ImgExt, ImgName
   ImgName := RegExReplace(ImgName, "[\W]")
   ImgName .= "_" . ImgExt
   GuiControl, , OutFile, %OutDir%\Create_%ImgName%.ahk
   IniWrite, %ImgDir%, %IniFile%, Image2Include, ImgDir
Return
; ----------------------------------------------------------------------------------------------------------------------
SelectFolder:
   Gui, +OwnDialogs
   FileSelectFolder, OutDir, *%OutDir%, 1, Select a folder to store the scripts:
   If (ErrorLevel)
      Return
   GuiControlGet, ImgFile
   GuiControl, , OutDir, %OutDir%
   If (ImgFile <> "") {
      GuiControl, , OutFile, %OutDir%\Create_%ImgName%.ahk
   }
   IniWrite, %OutDir%, %IniFile%, Image2Include, OutDir
Return
; ----------------------------------------------------------------------------------------------------------------------
Convert:
   SB_SetText("")
   Gui, Submit, NoHide
   If !FileExist(ImgFile) {
      GuiControl, Focus, BtnFile
      Return
   }
   If !InStr(FileExist(OutDir), "d") {
      GuiControl, Focus, BtnFolder
      Return
   }
   ; -------------------------------------------------------------------------------------------------------------------
   ; Read the image file
   ; -------------------------------------------------------------------------------------------------------------------
   File := FileOpen(ImgFile, "r")
   BinLen := File.Length
   File.RawRead(Bin, BinLen)
   File.Close()
   ; -------------------------------------------------------------------------------------------------------------------
   ; Encode the image file
   ; -------------------------------------------------------------------------------------------------------------------
   DllCall("Crypt32.dll\CryptBinaryToString", "Ptr", &Bin, "UInt", BinLen, "UInt", 0x01, "Ptr", 0, "UIntP", B64Len)
   VarSetCapacity(B64, B64Len << !!A_IsUnicode, 0)
   DllCall("Crypt32.dll\CryptBinaryToString", "Ptr", &Bin, "UInt", BinLen, "UInt", 0x01, "Ptr", &B64, "UIntP", B64Len)
   Bin := ""
   VarSetCapacity(Bin, 0)
   VarSetCapacity(B64, -1)
   B64 := RegExReplace(B64, "\r\n")
   B64Len := StrLen(B64)
   ; -------------------------------------------------------------------------------------------------------------------
   ; Write the AHK script
   ; -------------------------------------------------------------------------------------------------------------------
   PartLength := 16000
   CharsRead  := 1
   File := FileOpen(OutFile, "w", "UTF-8")
   File.Write(Header1 . "`r`nCreate_" . ImgName . "(NewHandle := False) {`r`n")
   If (CreateOnLoad)
      File.Write("Static hBitmap := Create_" . ImgName . "()`r`n")
   Else
      File.Write("Static hBitmap := 0`r`n")
   File.Write(Header2 . "`r`n")
   File.Write("VarSetCapacity(B64, " . StrLen(B64) . " << !!A_IsUnicode)`r`n")
   Part := "B64 := """
   While (CharsRead < B64Len) {
      File.Write(Part . SubStr(B64, CharsRead, PartLength) . """`r`n")
      CharsRead += PartLength
      If (CharsRead < B64Len)
         Part := "B64 .= """
   }
   File.Write(Footer1 . "`r`n")
   If (ReturnHICON)
      File.Write("DllCall(""Gdiplus.dll\GdipCreateHICONFromBitmap"", ""Ptr"", pBitmap, ""PtrP"", hBitmap, ""UInt"", 0)`r`n")
   Else
      File.Write("DllCall(""Gdiplus.dll\GdipCreateHBITMAPFromBitmap"", ""Ptr"", pBitmap, ""PtrP"", hBitmap, ""UInt"", 0)`r`n")
   File.Write(Footer2)
   File.Close()
   SB_SetText("  File " . OutFile . " successfully created!")
Return
; ======================================================================================================================
; Show the generated script
; ======================================================================================================================
Show:
   ; -------------------------------------------------------------------------------------------------------------------
   ; Reread the AHK script and show it
   ; -------------------------------------------------------------------------------------------------------------------
   FileRead, B64, %OutFile%
   If (ErrorLevel)
      Return
   FileGetSize, Size, %OutFile%
   Gui, Script: Default
   GuiControl, , ScriptEdit, %B64%
   ControlGet, Lines, LineCount, , , ahk_id %HEDIT%
   SB_SetText("  Size: " . Size . " bytes - Lines: " . Lines)
   Gui, Show, , %OutFile%
   Return
; ----------------------------------------------------------------------------------------------------------------------
ScriptGuiClose:
   GuiControl, , ShowEdit
   Gui, Hide
Return
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Compiling images into exe?

22 May 2016, 12:12

With the current compiler you can use FileInstall without writing the included file to disc. All you need to do is to ensure that the line containing FileInstall will never be executed. This will add a resource of type RCDATA and the name used for the source in FileInstall to the compiled script. You can find a German description and some script samples showing how to extract the image here.

After you've extracted the image you can use it with ImageSearch using the syntax posted by noname.
dice4321
Posts: 8
Joined: 21 May 2016, 21:44

Re: Compiling images into exe?

22 May 2016, 19:50

SifJar wrote:

Code: Select all

FileInstall, G:\KVC\New folder\Captures\capture(1).png, capture(1).png ; put in auto-execute section at top of script

; change the imagesearch commands like this:
ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *95 capture(1).png
Obviously do the same thing for each image

wow this is definitely something easy to do, thanks!
noname wrote:
how to compile the images into the .exe without the having .exe install the images
If you really need it ( i would prefer fileinstall like Sifjar showed) and ready to do some work :)

The latest version of ahk lets you use hbitmap handle instead of filename:
ImageSearch, x,y,0,0,a_screenwidth,a_screenheight,HBITMAP:*%hbitmap%
Once I'm done with the easy stuff, I'll move onto the hard haha, thank you thank you!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Google [Bot], Theda and 115 guests