Ho to use image files when building an executable

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jrderid
Posts: 1
Last visit: Sun Feb 21, 2016 12:13 am

Ho to use image files when building an executable

Post by jrderid »

Hi,

I am writing a GUI front end that locks and unlocks a richtextbox (locked is read only). I placed a button on the form and the picture of a lock in the button when it is read only. When the user presses the button, the script changes the richtextbox to readonly = $false, and I want to change the picture in the button to that of a lock that is unlocked.

My problem is that I am creating an executable file using the build option, and I don't understand conceptually how I can load an image file (the unlock picture) in preparation for someone clicking on the button. I cannot guarantee any directory structure on any computer but my own, so I cannot hard code the file path to the picture in the script.

I found some code that looked like it would do what I want if I don't build and executable, but how do I does this when I build an executable?

Here is the code that I found that I believe would work when I do not build an executable, but don't understand how to make something like this work when I do build an executable.

I am using SAPIEN PowerShell Studio 2015 and I am on the latest release.

Can you help?
  1. Add-Type -Assembly System.Drawing
  2.     $image = [System.Drawing.Image]::FromFile("C:\users\Administrator\Desktop\Avengers.jpg")
  3.     $button.Image = $image
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Ho to use image files when building an executable

Post by jvierra »

Icons are stored inside the script. You need to load an image list and use it to change the image.
Here is a example. I have also attached a small PSF that shows how to set it up.
  1. #----------------------------------------------
  2. #region Application Functions
  3. #----------------------------------------------
  4.  
  5. #endregion Application Functions
  6.  
  7. #----------------------------------------------
  8. # Generated Form Function
  9. #----------------------------------------------
  10. function Call-Demo-ButtonImages_psf {
  11.  
  12.     #----------------------------------------------
  13.     #region Import the Assemblies
  14.     #----------------------------------------------
  15.     [void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  16.     [void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  17.     [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  18.     [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  19.     [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  20.     [void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  21.     [void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  22.     [void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  23.     [void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  24.     #endregion Import Assemblies
  25.  
  26.     #----------------------------------------------
  27.     #region Generated Form Objects
  28.     #----------------------------------------------
  29.     [System.Windows.Forms.Application]::EnableVisualStyles()
  30.     $form1 = New-Object 'System.Windows.Forms.Form'
  31.     $button1 = New-Object 'System.Windows.Forms.Button'
  32.     $buttonOK = New-Object 'System.Windows.Forms.Button'
  33.     $imagelist1 = New-Object 'System.Windows.Forms.ImageList'
  34.     $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
  35.     #endregion Generated Form Objects
  36.  
  37.     #----------------------------------------------
  38.     # User Generated Script
  39.     #----------------------------------------------
  40.    
  41.     $form1_Load={
  42.         #TODO: Initialize Form Controls here
  43.        
  44.     }
  45.    
  46.     $button1_Click={
  47.        
  48.         $button1.ImageIndex = if($button1.ImageIndex){0}else{1}
  49.     }
  50.    
  51.     # --End User Generated Script--
  52.     #----------------------------------------------
  53.     #region Generated Events
  54.     #----------------------------------------------
  55.    
  56.     $Form_StateCorrection_Load=
  57.     {
  58.         #Correct the initial state of the form to prevent the .Net maximized form issue
  59.         $form1.WindowState = $InitialFormWindowState
  60.     }
  61.    
  62.     $Form_Cleanup_FormClosed=
  63.     {
  64.         #Remove all event handlers from the controls
  65.         try
  66.         {
  67.             $button1.remove_Click($button1_Click)
  68.             $form1.remove_Load($form1_Load)
  69.             $form1.remove_Load($Form_StateCorrection_Load)
  70.             $form1.remove_FormClosed($Form_Cleanup_FormClosed)
  71.         }
  72.         catch [Exception]
  73.         { }
  74.     }
  75.     #endregion Generated Events
  76.  
  77.     #----------------------------------------------
  78.     #region Generated Form Code
  79.     #----------------------------------------------
  80.     $form1.SuspendLayout()
  81.     #
  82.     # form1
  83.     #
  84.     $form1.Controls.Add($button1)
  85.     $form1.Controls.Add($buttonOK)
  86.     $form1.AcceptButton = $buttonOK
  87.     $form1.ClientSize = '284, 262'
  88.     $form1.FormBorderStyle = 'FixedDialog'
  89.     $form1.MaximizeBox = $False
  90.     $form1.MinimizeBox = $False
  91.     $form1.Name = 'form1'
  92.     $form1.StartPosition = 'CenterScreen'
  93.     $form1.Text = 'Form'
  94.     $form1.add_Load($form1_Load)
  95.     #
  96.     # button1
  97.     #
  98.     $button1.ImageIndex = 0
  99.     $button1.ImageList = $imagelist1
  100.     $button1.Location = '105, 55'
  101.     $button1.Name = 'button1'
  102.     $button1.Size = '75, 75'
  103.     $button1.TabIndex = 1
  104.     $button1.TextImageRelation = 'ImageAboveText'
  105.     $button1.UseVisualStyleBackColor = $True
  106.     $button1.add_Click($button1_Click)
  107.     #
  108.     # buttonOK
  109.     #
  110.     $buttonOK.Anchor = 'Bottom, Right'
  111.     $buttonOK.DialogResult = 'OK'
  112.     $buttonOK.Location = '197, 227'
  113.     $buttonOK.Name = 'buttonOK'
  114.     $buttonOK.Size = '75, 23'
  115.     $buttonOK.TabIndex = 0
  116.     $buttonOK.Text = '&OK'
  117.     $buttonOK.UseVisualStyleBackColor = $True
  118.     #
  119.     # imagelist1
  120.     #
  121.     $Formatter_binaryFomatter = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
  122.     #region Binary Data
  123.     $System_IO_MemoryStream = New-Object System.IO.MemoryStream (,[byte[]][System.Convert]::FromBase64String('
  124. AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAu
  125. MC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAA
  126. ACZTeXN0ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkD
  127. AAAADwMAAABADQAAAk1TRnQBSQFMAgEBAgEAAQgBAAEIAQABQAEAAUABAAT/AQkBAAj/AUIBTQE2
  128. AQQGAAE2AQQCAAEoBAABAQIAAUADAAEBAQABCAYAAUAYAAGAAgABgAMAAoABAAGAAwABgAEAAYAB
  129. AAKAAgADwAEAAcAB3AHAAQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEA
  130. AykBAANVAQADTQEAA0IBAAM5AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8B
  131. AAHWAucBAAGQAakBrQIAAf8BMwMAAWYDAAGZAwABzAIAATMDAAIzAgABMwFmAgABMwGZAgABMwHM
  132. AgABMwH/AgABZgMAAWYBMwIAAmYCAAFmAZkCAAFmAcwCAAFmAf8CAAGZAwABmQEzAgABmQFmAgAC
  133. mQIAAZkBzAIAAZkB/wIAAcwDAAHMATMCAAHMAWYCAAHMAZkCAALMAgABzAH/AgAB/wFmAgAB/wGZ
  134. AgAB/wHMAQABMwH/AgAB/wEAATMBAAEzAQABZgEAATMBAAGZAQABMwEAAcwBAAEzAQAB/wEAAf8B
  135. MwIAAzMBAAIzAWYBAAIzAZkBAAIzAcwBAAIzAf8BAAEzAWYCAAEzAWYBMwEAATMCZgEAATMBZgGZ
  136. AQABMwFmAcwBAAEzAWYB/wEAATMBmQIAATMBmQEzAQABMwGZAWYBAAEzApkBAAEzAZkBzAEAATMB
  137. mQH/AQABMwHMAgABMwHMATMBAAEzAcwBZgEAATMBzAGZAQABMwLMAQABMwHMAf8BAAEzAf8BMwEA
  138. ATMB/wFmAQABMwH/AZkBAAEzAf8BzAEAATMC/wEAAWYDAAFmAQABMwEAAWYBAAFmAQABZgEAAZkB
  139. AAFmAQABzAEAAWYBAAH/AQABZgEzAgABZgIzAQABZgEzAWYBAAFmATMBmQEAAWYBMwHMAQABZgEz
  140. Af8BAAJmAgACZgEzAQADZgEAAmYBmQEAAmYBzAEAAWYBmQIAAWYBmQEzAQABZgGZAWYBAAFmApkB
  141. AAFmAZkBzAEAAWYBmQH/AQABZgHMAgABZgHMATMBAAFmAcwBmQEAAWYCzAEAAWYBzAH/AQABZgH/
  142. AgABZgH/ATMBAAFmAf8BmQEAAWYB/wHMAQABzAEAAf8BAAH/AQABzAEAApkCAAGZATMBmQEAAZkB
  143. AAGZAQABmQEAAcwBAAGZAwABmQIzAQABmQEAAWYBAAGZATMBzAEAAZkBAAH/AQABmQFmAgABmQFm
  144. ATMBAAGZATMBZgEAAZkBZgGZAQABmQFmAcwBAAGZATMB/wEAApkBMwEAApkBZgEAA5kBAAKZAcwB
  145. AAKZAf8BAAGZAcwCAAGZAcwBMwEAAWYBzAFmAQABmQHMAZkBAAGZAswBAAGZAcwB/wEAAZkB/wIA
  146. AZkB/wEzAQABmQHMAWYBAAGZAf8BmQEAAZkB/wHMAQABmQL/AQABzAMAAZkBAAEzAQABzAEAAWYB
  147. AAHMAQABmQEAAcwBAAHMAQABmQEzAgABzAIzAQABzAEzAWYBAAHMATMBmQEAAcwBMwHMAQABzAEz
  148. Af8BAAHMAWYCAAHMAWYBMwEAAZkCZgEAAcwBZgGZAQABzAFmAcwBAAGZAWYB/wEAAcwBmQIAAcwB
  149. mQEzAQABzAGZAWYBAAHMApkBAAHMAZk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v8B9QYZAeEB2gjZA9qAAAFGA0wKbwEaAfQB8wGZ
  176. BW8CFgEbBP8B9gEaARYBmgH1BP8B8wEWAW8BFgNvAZkC8wGaCm8ETATaCNkB2gEZIv8C9QH/AeIB
  177. 2wjZA9qAAAJGAkwKbwEaAvUBmgVvARYBbwGTA/8BGwGTAnQBFgGTARoD/wGTBm8BGgL/ARoKbwJM
  178. AkYH2gbZAbkBGRz/AhkD/wHzARkB3AHaAtkB2gPZAdoC2QPagAACRgRMFG8BGgH1AZoBdAZvAZkB
  179. 9gHzCG8CFglvBEwCRgXaCdkB2gHbARkB9Qb/AfUB4gEZAeEFGQH0AfUC/wL1AhkB4QHoAtoD2wLa
  180. CtkE2oAABEYDTBJvAZoBGwEaCW8BkwEbARoBFhBvBEwDRgbaC9kB2gHbAdwB4QPbAdoJ2QXaEtkH
  181. 2oAABUYDTBBvAZMD/wGTCG8BGgL/AZoPbwNMBUYG2gLZAdov2QjagAAGRgRMD28BkwGaAZMKbwKT
  182. Dm8ETAZGC9os2QnagAAHRgZMKG8DTAhGC9oo2QHaAtkK2oAACUYFTCRvBUwJRg3aJdkO2oAADEYF
  183. TB5vBUwMRg/aAdkB2h/ZENqAAA5GBkwYbwVMD0YS2hbZAtoC2RTagAASRgpMCG8KTBJGGNoC2QLa
  184. CNkc2oAAFkYUTBZGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqA
  185. AEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAAFCAU0BPgcAAT4DAAEo
  186. BAABAQIAAUADAAEBAQABAQYAAQgWAAP//wD/AP8A/wD/AP8A/wD/AAkACw=='))
  187.     #endregion
  188.     $imagelist1.ImageStream = $Formatter_binaryFomatter.Deserialize($System_IO_MemoryStream)
  189.     $Formatter_binaryFomatter = $null
  190.     $System_IO_MemoryStream = $null
  191.     $imagelist1.TransparentColor = 'Transparent'
  192.     $form1.ResumeLayout()
  193.     #endregion Generated Form Code
  194.  
  195.     #----------------------------------------------
  196.  
  197.     #Save the initial state of the form
  198.     $InitialFormWindowState = $form1.WindowState
  199.     #Init the OnLoad event to correct the initial state of the form
  200.     $form1.add_Load($Form_StateCorrection_Load)
  201.     #Clean up the control events
  202.     $form1.add_FormClosed($Form_Cleanup_FormClosed)
  203.     #Show the Form
  204.     return $form1.ShowDialog()
  205.  
  206. } #End Function
  207.  
  208. #Call the form
  209. Call-Demo-ButtonImages_psf | Out-Null
Attachments
Demo-ButtonImages.psf
(41.79 KiB) Downloaded 252 times
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: Ho to use image files when building an executable

Post by monoeagle »

Hi jvierra

thanks for this example. How do you know which assembly do you have to add?

greetz
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Ho to use image files when building an executable

Post by jvierra »

It is done automatically by PowerShell Studio. I only posted the code so you can see that it is embedded into the script. Download the PSF and you will see there is no code to add. Just add an image control and the images and assign them in the button click.

This is the total amount of code used:
  1. $button1_Click={
  2.     $button1.ImageIndex = if($button1.ImageIndex){0}else{1}
  3. }
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked