如何在 PowerShell GUI 可执行文件中嵌入图标? [英] How to embed an icon in a PowerShell GUI executable?

查看:90
本文介绍了如何在 PowerShell GUI 可执行文件中嵌入图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 powershell gui,我想在 windows.form 中插入一个图标.
我是这样做的,我用ps2exe生成了一个exe文件.

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

如果我将带有图标 test.ico 的 dir 图标与我的 exe 一起带上,一切都会运行良好,但现在我会将图标合并到我的代码中,而不必将图标目录带上我的 exe.

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?

推荐答案

您可以使用如下所示的 base64 编码图像在代码中嵌入图形信息:

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)
$stream          = New-Object IO.MemoryStream($iconBytes, 0, $iconBytes.Length)
$stream.Write($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 form
$Form.Dispose()

要将您自己的图像转换为 base64 字符串,有很多在线转换器,例如 这个.

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

相反(将 base64 图像数据转换回图形图像),他们还有一个页面用于 这里

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

当然您也可以使用 Powershell 进行 Base64 的转换:

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

[Convert]::ToBase64String((Get-Content ".\icon\test.ico" -Encoding Byte))

这篇关于如何在 PowerShell GUI 可执行文件中嵌入图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆