Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed an icon in a PowerShell GUI executable?

Tags:

powershell

I created a powershell gui and I would like to insert an icon to my windows.form.
I did it this way and I generated an exe file with ps2exe.

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

#region begin GUI{ 

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '400,230'
$Form.text                       = "Test"
$Form.TopMost                    = $false
$Icon                            = New-Object system.drawing.icon (".\icon\test.ico")
$Form.Icon                       = $Icon

Everything works well if I bring along with my exe the dir icon with the icon test.ico but now I would incorporate the icon in my code without having to bring the icon directory with my exe.

Is it possible to do it? If so, how?

like image 810
Gus Avatar asked Nov 19 '18 14:11

Gus


People also ask

How do I change the icon of a PowerShell script?

Find a shortcut somewhere, right-click that shortcut, and then click Properties. On the Shortcut tab, click Change Icon. After setting the icon location and specifying the icon index, you call the Save method and – just like that – your shortcut icon will be changed.

How do I get the PowerShell icon?

Powershell can be found by right clicking on the start button & it should show it the list, from there you can right click on it & choose properties to find where it is stored on your system. Or click on the search icon in the taskbar & enter powershell which will also find the program.

Can you create a UI with PowerShell?

However, PowerShell is a powerful and modern automation tool for Windows that allows you transparently use a variety of . NET Framework objects. For example, using the . NET API, you can easily create a simple graphical interface (GUI) for your PowerShell scripts.

What is PowerShell GUI?

A PowerShell graphical user interface (GUI) can help lower-XP PowerShell users to interact more safely and confidently with PowerShell. GUIs are great, because they: let users run PowerShell by themselves. provide a job-specific interface to enforce limited input.


1 Answers

You can embed graphic information in your code by using a base64 encoded image like below:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form            = New-Object system.Windows.Forms.Form
$Form.ClientSize = '400,230'
$Form.text       = "Test"
$Form.TopMost    = $false

# This base64 string holds the bytes that make up the orange 'G' icon (just an example for a 32x32 pixel image)
$iconBase64      = 'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuMTnU1rJkAAAB50lEQVRIS7WWzytEURTHZ2FhaWFhYWFhYWFhaWFh6c+wsGCapJBJU0hRSrOgLBVSmkQoSpqyUJISapIFJU0i1KQp1PG9826vO9+Z97Pr22dz3pxzv/PO/fUSkvxfOLYOx9bh2DocBzPZKlPtku2VuS7JtMlwIydUw7EnGO50Rd4ehPRdlru87GWUMZVU4LgOqLza0cP56PdHNga4NthgsUc+i3qIQOH9qDzAYLZTyiVd7Ah/8/lGztclNyLbY5JfksKxeuiKRvAzwOy93OsyR+9Pam6HajLHm5UfXhTQT34GqDH1eCGjTZxjkmqQmQ5+6Gdgth5NQLsoIRwca7AoTZ1k1UM0p7Y/QXCsof4sdOvRrRlgeZgyu49eY/0cTDO76ShzcJnTQ0O0NvA2XioWqjIrcKy5PdQ1kLl90CKsVC9F2GiYVVPuiQaDiRb1TzGWw9eHzoEiGGwO6hpHWFRe04vuu4pggCPIFPwowSWmAXa/qdKr5zaOaQBwyps6W+UEh/gGtasFlrjCKC2+ATia15WucHpf76vna/2ylVLntnmeRzbApsUQ4RXZwAFnAC7eMMLNSrWhDEC6RfUac1D3+sRew87HhVzvC4PjYLBecRxhDsByn/qEoYRqOLYOx9bh2DocWyaZ+APgBBKhVfsHwAAAAABJRU5ErkJggg=='
$iconBytes       = [Convert]::FromBase64String($iconBase64)
# initialize a Memory stream holding the bytes
$stream          = [System.IO.MemoryStream]::new($iconBytes, 0, $iconBytes.Length)
$Form.Icon       = [System.Drawing.Icon]::FromHandle(([System.Drawing.Bitmap]::new($stream).GetHIcon()))

# PowerShell versions older than 5.0 use this:
# $stream        = New-Object IO.MemoryStream($iconBytes, 0, $iconBytes.Length)
# $Form.Icon     = [System.Drawing.Icon]::FromHandle((New-Object System.Drawing.Bitmap -Argument $stream).GetHIcon())

[void]$Form.ShowDialog()

# when done, dispose of the stream and form
$stream.Dispose()
$Form.Dispose()

To convert your own image to a base64 string, there are lots of online converters like this one.

To go the other way around (convert base64 image data back to a graphic image) they also have a page for that here

Of course you can also do the conversion to Base64 using Powershell:

[Convert]::ToBase64String((Get-Content ".\icon\test.ico" -Encoding Byte))
like image 50
Theo Avatar answered Sep 21 '22 22:09

Theo