3

I use the PowerGUI editor to convert a ps1 file to an exe file. Reason is that I dont want people to see my source code. The script includes a own little GUI with a picture on it. My problem is that after converting the script to an exe file the picture will only be shown when it exists on a specific path. If I delete or move the picture from that path it wont be shown when starting the exe.

How can I include the picture to the exe? I want to have only one file in the end ...

1 Answers1

1

One way you could do this is by converting your image into a Base 64 String, using the following:

[convert]::ToBase64String((get-content C:\YourPicture.jpg -encoding byte)) > C:\YourString.txt

With the string that is produced in the text file "C:\YourString.txt" you can copy and paste it into your code and load it into a picture object on the form like so:

$logo.Image = [System.Convert]::FromBase64String('
iVBORw0KGgoAAAANSUhEUgAAAfQAAACMCAYAAACK0FuSAAAABGdBTUEAALGPC/xhBQAAAAlwSFlz
AAAXEQAAFxEByibzPwAAAAd0SU1FB98DFA8VLc5RQx4AANRpSURBVHhe7J0FmBTX0oZ7F1jc3d3d
nU+dOtXw4MGDZfft25fmyJEjPu+//76Xqzke8pCHPOQhD3lIQGxxcGSapvHdd9+FF3hHEdjGFHgn
          .......      Many more lines of string   .......
OqelFQzDMAzD/CZoztADbUwhUm0JERoXCNfEQYhyPAQryiBIUQSBiiTwl1sb3skwDMMwzG+KWLVe
jTEBH6U7JvJ0CJQXoaGPQMWBm5W+Va27k14MwzDMfwCA/wfUstOLO+nBIAAAAABJRU5Jggg==')

Do this will mean that your image is stored within the code already and doesn't need to be loaded from somewhere.

Note: Make sure that the pictures size on disk is the smallest you can get it as producing the string can take sometime and could turn out thousands of lines long. So I would recommend that you only use a pic that is less that 75 Kilobytes in size. You could do it with larger one but this will take a long time to process.

Richard
  • 6,812
  • 5
  • 45
  • 60
  • Thank you! It works fine and solves my problem easily. I could never image a solution like this before, are commercial programmers storing content to their exe files like this as well? Would be interesting. Only disadvantage is the size of the script after pasting the BaseString into it: Picture has ~500 KB, BaseString has ~1500 KB. But I can live with that ;-) –  Sep 07 '15 at 05:11
  • @Hamstibamsti Could you accept the answer please and yes I would image that there are some commercial programmers using similar techniques to store items within scripts. There many well be a other slightly shorter format that you could uses but I'm not aware of one in `PowerShell`. – Richard Sep 07 '15 at 08:10