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.
Locked
User avatar
jrderid
Posts: 1
Joined: Thu Aug 14, 2014 9:27 am

Ho to use image files when building an executable

Post by jrderid » Sat Feb 06, 2016 2:02 am

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.  
  3.     $image = [System.Drawing.Image]::FromFile("C:\users\Administrator\Desktop\Avengers.jpg")
  4.  
  5.     $button.Image = $image

User avatar
jvierra
Posts: 11559
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Ho to use image files when building an executable

Post by jvierra » Sat Feb 06, 2016 2:48 am

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.  
  3. #region Application Functions
  4.  
  5. #----------------------------------------------
  6.  
  7.  
  8.  
  9. #endregion Application Functions
  10.  
  11.  
  12.  
  13. #----------------------------------------------
  14.  
  15. # Generated Form Function
  16.  
  17. #----------------------------------------------
  18.  
  19. function Call-Demo-ButtonImages_psf {
  20.  
  21.  
  22.  
  23.     #----------------------------------------------
  24.  
  25.     #region Import the Assemblies
  26.  
  27.     #----------------------------------------------
  28.  
  29.     [void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  30.  
  31.     [void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  32.  
  33.     [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  34.  
  35.     [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  36.  
  37.     [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  38.  
  39.     [void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  40.  
  41.     [void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  42.  
  43.     [void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  44.  
  45.     [void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  46.  
  47.     #endregion Import Assemblies
  48.  
  49.  
  50.  
  51.     #----------------------------------------------
  52.  
  53.     #region Generated Form Objects
  54.  
  55.     #----------------------------------------------
  56.  
  57.     [System.Windows.Forms.Application]::EnableVisualStyles()
  58.  
  59.     $form1 = New-Object 'System.Windows.Forms.Form'
  60.  
  61.     $button1 = New-Object 'System.Windows.Forms.Button'
  62.  
  63.     $buttonOK = New-Object 'System.Windows.Forms.Button'
  64.  
  65.     $imagelist1 = New-Object 'System.Windows.Forms.ImageList'
  66.  
  67.     $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
  68.  
  69.     #endregion Generated Form Objects
  70.  
  71.  
  72.  
  73.     #----------------------------------------------
  74.  
  75.     # User Generated Script
  76.  
  77.     #----------------------------------------------
  78.  
  79.    
  80.  
  81.     $form1_Load={
  82.  
  83.         #TODO: Initialize Form Controls here
  84.  
  85.        
  86.  
  87.     }
  88.  
  89.    
  90.  
  91.     $button1_Click={
  92.  
  93.        
  94.  
  95.         $button1.ImageIndex = if($button1.ImageIndex){0}else{1}
  96.  
  97.     }
  98.  
  99.    
  100.  
  101.     # --End User Generated Script--
  102.  
  103.     #----------------------------------------------
  104.  
  105.     #region Generated Events
  106.  
  107.     #----------------------------------------------
  108.  
  109.    
  110.  
  111.     $Form_StateCorrection_Load=
  112.  
  113.     {
  114.  
  115.         #Correct the initial state of the form to prevent the .Net maximized form issue
  116.  
  117.         $form1.WindowState = $InitialFormWindowState
  118.  
  119.     }
  120.  
  121.    
  122.  
  123.     $Form_Cleanup_FormClosed=
  124.  
  125.     {
  126.  
  127.         #Remove all event handlers from the controls
  128.  
  129.         try
  130.  
  131.         {
  132.  
  133.             $button1.remove_Click($button1_Click)
  134.  
  135.             $form1.remove_Load($form1_Load)
  136.  
  137.             $form1.remove_Load($Form_StateCorrection_Load)
  138.  
  139.             $form1.remove_FormClosed($Form_Cleanup_FormClosed)
  140.  
  141.         }
  142.  
  143.         catch [Exception]
  144.  
  145.         { }
  146.  
  147.     }
  148.  
  149.     #endregion Generated Events
  150.  
  151.  
  152.  
  153.     #----------------------------------------------
  154.  
  155.     #region Generated Form Code
  156.  
  157.     #----------------------------------------------
  158.  
  159.     $form1.SuspendLayout()
  160.  
  161.     #
  162.  
  163.     # form1
  164.  
  165.     #
  166.  
  167.     $form1.Controls.Add($button1)
  168.  
  169.     $form1.Controls.Add($buttonOK)
  170.  
  171.     $form1.AcceptButton = $buttonOK
  172.  
  173.     $form1.ClientSize = '284, 262'
  174.  
  175.     $form1.FormBorderStyle = 'FixedDialog'
  176.  
  177.     $form1.MaximizeBox = $False
  178.  
  179.     $form1.MinimizeBox = $False
  180.  
  181.     $form1.Name = 'form1'
  182.  
  183.     $form1.StartPosition = 'CenterScreen'
  184.  
  185.     $form1.Text = 'Form'
  186.  
  187.     $form1.add_Load($form1_Load)
  188.  
  189.     #
  190.  
  191.     # button1
  192.  
  193.     #
  194.  
  195.     $button1.ImageIndex = 0
  196.  
  197.     $button1.ImageList = $imagelist1
  198.  
  199.     $button1.Location = '105, 55'
  200.  
  201.     $button1.Name = 'button1'
  202.  
  203.     $button1.Size = '75, 75'
  204.  
  205.     $button1.TabIndex = 1
  206.  
  207.     $button1.TextImageRelation = 'ImageAboveText'
  208.  
  209.     $button1.UseVisualStyleBackColor = $True
  210.  
  211.     $button1.add_Click($button1_Click)
  212.  
  213.     #
  214.  
  215.     # buttonOK
  216.  
  217.     #
  218.  
  219.     $buttonOK.Anchor = 'Bottom, Right'
  220.  
  221.     $buttonOK.DialogResult = 'OK'
  222.  
  223.     $buttonOK.Location = '197, 227'
  224.  
  225.     $buttonOK.Name = 'buttonOK'
  226.  
  227.     $buttonOK.Size = '75, 23'
  228.  
  229.     $buttonOK.TabIndex = 0
  230.  
  231.     $buttonOK.Text = '&OK'
  232.  
  233.     $buttonOK.UseVisualStyleBackColor = $True
  234.  
  235.     #
  236.  
  237.     # imagelist1
  238.  
  239.     #
  240.  
  241.     $Formatter_binaryFomatter = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
  242.  
  243.     #region Binary Data
  244.  
  245.     $System_IO_MemoryStream = New-Object System.IO.MemoryStream (,[byte[]][System.Convert]::FromBase64String('
  246.  
  247. AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAu
  248.  
  249. MC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAA
  250.  
  251. ACZTeXN0ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkD
  252.  
  253. AAAADwMAAABADQAAAk1TRnQBSQFMAgEBAgEAAQgBAAEIAQABQAEAAUABAAT/AQkBAAj/AUIBTQE2
  254.  
  255. AQQGAAE2AQQCAAEoBAABAQIAAUADAAEBAQABCAYAAUAYAAGAAgABgAMAAoABAAGAAwABgAEAAYAB
  256.  
  257. AAKAAgADwAEAAcAB3AHAAQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEA
  258.  
  259. AykBAANVAQADTQEAA0IBAAM5AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8B
  260.  
  261. AAHWAucBAAGQAakBrQIAAf8BMwMAAWYDAAGZAwABzAIAATMDAAIzAgABMwFmAgABMwGZAgABMwHM
  262.  
  263. AgABMwH/AgABZgMAAWYBMwIAAmYCAAFmAZkCAAFmAcwCAAFmAf8CAAGZAwABmQEzAgABmQFmAgAC
  264.  
  265. mQIAAZkBzAIAAZkB/wIAAcwDAAHMATMCAAHMAWYCAAHMAZkCAALMAgABzAH/AgAB/wFmAgAB/wGZ
  266.  
  267. AgAB/wHMAQABMwH/AgAB/wEAATMBAAEzAQABZgEAATMBAAGZAQABMwEAAcwBAAEzAQAB/wEAAf8B
  268.  
  269. MwIAAzMBAAIzAWYBAAIzAZkBAAIzAcwBAAIzAf8BAAEzAWYCAAEzAWYBMwEAATMCZgEAATMBZgGZ
  270.  
  271. AQABMwFmAcwBAAEzAWYB/wEAATMBmQIAATMBmQEzAQABMwGZAWYBAAEzApkBAAEzAZkBzAEAATMB
  272.  
  273. mQH/AQABMwHMAgABMwHMATMBAAEzAcwBZgEAATMBzAGZAQABMwLMAQABMwHMAf8BAAEzAf8BMwEA
  274.  
  275. ATMB/wFmAQABMwH/AZkBAAEzAf8BzAEAATMC/wEAAWYDAAFmAQABMwEAAWYBAAFmAQABZgEAAZkB
  276.  
  277. AAFmAQABzAEAAWYBAAH/AQABZgEzAgABZgIzAQABZgEzAWYBAAFmATMBmQEAAWYBMwHMAQABZgEz
  278.  
  279. Af8BAAJmAgACZgEzAQADZgEAAmYBmQEAAmYBzAEAAWYBmQIAAWYBmQEzAQABZgGZAWYBAAFmApkB
  280.  
  281. AAFmAZkBzAEAAWYBmQH/AQABZgHMAgABZgHMATMBAAFmAcwBmQEAAWYCzAEAAWYBzAH/AQABZgH/
  282.  
  283. AgABZgH/ATMBAAFmAf8BmQEAAWYB/wHMAQABzAEAAf8BAAH/AQABzAEAApkCAAGZATMBmQEAAZkB
  284.  
  285. AAGZAQABmQEAAcwBAAGZAwABmQIzAQABmQEAAWYBAAGZATMBzAEAAZkBAAH/AQABmQFmAgABmQFm
  286.  
  287. ATMBAAGZATMBZgEAAZkBZgGZAQABmQFmAcwBAAGZATMB/wEAApkBMwEAApkBZgEAA5kBAAKZAcwB
  288.  
  289. AAKZAf8BAAGZAcwCAAGZAcwBMwEAAWYBzAFmAQABmQHMAZkBAAGZAswBAAGZAcwB/wEAAZkB/wIA
  290.  
  291. AZkB/wEzAQABmQHMAWYBAAGZAf8BmQEAAZkB/wHMAQABmQL/AQABzAMAAZkBAAEzAQABzAEAAWYB
  292.  
  293. AAHMAQABmQEAAcwBAAHMAQABmQEzAgABzAIzAQABzAEzAWYBAAHMATMBmQEAAcwBMwHMAQABzAEz
  294.  
  295. Af8BAAHMAWYCAAHMAWYBMwEAAZkCZgEAAcwBZgGZAQABzAFmAcwBAAGZAWYB/wEAAcwBmQIAAcwB
  296.  
  297. mQEzAQABzAGZAWYBAAHMApkBAAHMAZkBzAEAAcwBmQH/AQACzAIAAswBMwEAAswBZgEAAswBmQEA
  298.  
  299. A8wBAALMAf8BAAHMAf8CAAHMAf8BMwEAAZkB/wFmAQABzAH/AZkBAAHMAf8BzAEAAcwC/wEAAcwB
  300.  
  301. AAEzAQAB/wEAAWYBAAH/AQABmQEAAcwBMwIAAf8CMwEAAf8BMwFmAQAB/wEzAZkBAAH/ATMBzAEA
  302.  
  303. Af8BMwH/AQAB/wFmAgAB/wFmATMBAAHMAmYBAAH/AWYBmQEAAf8BZgHMAQABzAFmAf8BAAH/AZkC
  304.  
  305. AAH/AZkBMwEAAf8BmQFmAQAB/wKZAQAB/wGZAcwBAAH/AZkB/wEAAf8BzAIAAf8BzAEzAQAB/wHM
  306.  
  307. AWYBAAH/AcwBmQEAAf8CzAEAAf8BzAH/AQAC/wEzAQABzAH/AWYBAAL/AZkBAAL/AcwBAAJmAf8B
  308.  
  309. AAFmAf8BZgEAAWYC/wEAAf8CZgEAAf8BZgH/AQAC/wFmAQABIQEAAaUBAANfAQADdwEAA4YBAAOW
  310.  
  311. AQADywEAA7IBAAPXAQAD3QEAA+MBAAPqAQAD8QEAA/gBAAHwAfsB/wEAAaQCoAEAA4ADAAH/AgAB
  312.  
  313. /wMAAv8BAAH/AwAB/wEAAf8BAAL/AgAD/wEAQEZA2oAAQEZA2oAAQEZA2oAAQEZA2oAAQEZA2oAA
  314.  
  315. QEZA2oAAQEZA2oAAQEZA2oAAQEZA2oAAQEZA2oAAQEZA2oAAQEZA2oAAQEZA2oAAQEZA2oAAFkYV
  316.  
  317. TBVGQNqAABFGCUwMbwlMEUYW2hLZAtoB2RXagAAORgZMGG8GTA5GEtoG2QLaFNkS2oAADEYFTB9v
  318.  
  319. BUwLRg7aI9kP2oAACUYETCZvBEwJRg3aJdkC2gHZC9qAAAhGBEwpbwRMB0YN2inZCtqAAAZGBEws
  320.  
  321. bwRMBkYK2grZAtoi2QjagAAFRgNMMG8ETARGCdoH2RvaAdkF2gbZCdqAAANGBEwVbwgWFW8ETANG
  322.  
  323. BdoI2SXaCNkG2oAAAkYETBFvExYQbwRMAkYF2gfZKNoF2QfagAACRgJMEG8QFgN1BRYQbwNMAUYE
  324.  
  325. 2gjZENoI3wjaENkE2oAAAUYDTBBvAhYBdRUWEG8DTAFGAtoB2QHaCdkL2g/fAdoV2QHaAtmAAANM
  326.  
  327. Em8GFgt1BhYRbwNMA9oC2QHaCdkB2gLbAegK3wLoB98X2QPagAADTBFvAXQEFgF1DJQBdQQWAXQS
  328.  
  329. bwJMAdoO2QHaB+gC3wHZAf4B2QXoAd8B/gHfAuga2YAAAkwRbwMWFZQBdQIWEG8CTALaDdkB2gfo
  330.  
  331. AdoE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AbkBGRz/AhkD/wHzARkB3AHaAtkB2gPZAdoC2QPagAACRgRMFG8BGgH1AZoBdAZvAZkB
  356.  
  357. 9gHzCG8CFglvBEwCRgXaCdkB2gHbARkB9Qb/AfUB4gEZAeEFGQH0AfUC/wL1AhkB4QHoAtoD2wLa
  358.  
  359. CtkE2oAABEYDTBJvAZoBGwEaCW8BkwEbARoBFhBvBEwDRgbaC9kB2gHbAdwB4QPbAdoJ2QXaEtkH
  360.  
  361. 2oAABUYDTBBvAZMD/wGTCG8BGgL/AZoPbwNMBUYG2gLZAdov2QjagAAGRgRMD28BkwGaAZMKbwKT
  362.  
  363. Dm8ETAZGC9os2QnagAAHRgZMKG8DTAhGC9oo2QHaAtkK2oAACUYFTCRvBUwJRg3aJdkO2oAADEYF
  364.  
  365. TB5vBUwMRg/aAdkB2h/ZENqAAA5GBkwYbwVMD0YS2hbZAtoC2RTagAASRgpMCG8KTBJGGNoC2QLa
  366.  
  367. CNkc2oAAFkYUTBZGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqA
  368.  
  369. AEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAAFCAU0BPgcAAT4DAAEo
  370.  
  371. BAABAQIAAUADAAEBAQABAQYAAQgWAAP//wD/AP8A/wD/AP8A/wD/AAkACw=='))
  372.  
  373.     #endregion
  374.  
  375.     $imagelist1.ImageStream = $Formatter_binaryFomatter.Deserialize($System_IO_MemoryStream)
  376.  
  377.     $Formatter_binaryFomatter = $null
  378.  
  379.     $System_IO_MemoryStream = $null
  380.  
  381.     $imagelist1.TransparentColor = 'Transparent'
  382.  
  383.     $form1.ResumeLayout()
  384.  
  385.     #endregion Generated Form Code
  386.  
  387.  
  388.  
  389.     #----------------------------------------------
  390.  
  391.  
  392.  
  393.     #Save the initial state of the form
  394.  
  395.     $InitialFormWindowState = $form1.WindowState
  396.  
  397.     #Init the OnLoad event to correct the initial state of the form
  398.  
  399.     $form1.add_Load($Form_StateCorrection_Load)
  400.  
  401.     #Clean up the control events
  402.  
  403.     $form1.add_FormClosed($Form_Cleanup_FormClosed)
  404.  
  405.     #Show the Form
  406.  
  407.     return $form1.ShowDialog()
  408.  
  409.  
  410.  
  411. } #End Function
  412.  
  413.  
  414.  
  415. #Call the form
  416.  
  417. Call-Demo-ButtonImages_psf | Out-Null
Attachments
Demo-ButtonImages.psf
(41.79 KiB) Downloaded 85 times

User avatar
monoeagle
Posts: 107
Joined: Tue Oct 13, 2015 9:10 am

Re: Ho to use image files when building an executable

Post by monoeagle » Sun Feb 07, 2016 10:08 am

Hi jvierra

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

greetz

User avatar
jvierra
Posts: 11559
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Ho to use image files when building an executable

Post by jvierra » Sun Feb 07, 2016 10:18 am

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.  
  3.     $button1.ImageIndex = if($button1.ImageIndex){0}else{1}
  4.  
  5. }

Locked