Powershell how to embed icon in powershell GUI exe











up vote
0
down vote

favorite
1












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 (".icontest.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.

It's possible to do it? How?



Thank you










share|improve this question




























    up vote
    0
    down vote

    favorite
    1












    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 (".icontest.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.

    It's possible to do it? How?



    Thank you










    share|improve this question


























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      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 (".icontest.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.

      It's possible to do it? How?



      Thank you










      share|improve this question















      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 (".icontest.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.

      It's possible to do it? How?



      Thank you







      powershell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 14:25

























      asked Nov 19 at 14:13









      Gus

      4231519




      4231519
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          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);
          $iconImage = [System.Drawing.Image]::FromStream($stream, $true)
          $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()


          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 ".icontest.ico" -Encoding Byte))





          share|improve this answer





















          • thank you is what I was looking for
            – Gus
            Nov 19 at 15:20











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376491%2fpowershell-how-to-embed-icon-in-powershell-gui-exe%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          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);
          $iconImage = [System.Drawing.Image]::FromStream($stream, $true)
          $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()


          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 ".icontest.ico" -Encoding Byte))





          share|improve this answer





















          • thank you is what I was looking for
            – Gus
            Nov 19 at 15:20















          up vote
          6
          down vote



          accepted










          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);
          $iconImage = [System.Drawing.Image]::FromStream($stream, $true)
          $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()


          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 ".icontest.ico" -Encoding Byte))





          share|improve this answer





















          • thank you is what I was looking for
            – Gus
            Nov 19 at 15:20













          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          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);
          $iconImage = [System.Drawing.Image]::FromStream($stream, $true)
          $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()


          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 ".icontest.ico" -Encoding Byte))





          share|improve this answer












          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);
          $iconImage = [System.Drawing.Image]::FromStream($stream, $true)
          $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()


          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 ".icontest.ico" -Encoding Byte))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 14:56









          Theo

          2,9911518




          2,9911518












          • thank you is what I was looking for
            – Gus
            Nov 19 at 15:20


















          • thank you is what I was looking for
            – Gus
            Nov 19 at 15:20
















          thank you is what I was looking for
          – Gus
          Nov 19 at 15:20




          thank you is what I was looking for
          – Gus
          Nov 19 at 15:20


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376491%2fpowershell-how-to-embed-icon-in-powershell-gui-exe%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin