Support Forums

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

Ho to use image files when building an executable

Postby 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?

Add-Type -Assembly System.Drawing
$image = [System.Drawing.Image]::FromFile("C:\users\Administrator\Desktop\Avengers.jpg")
$button.Image = $image
User avatar
jvierra
Posts: 10030
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Ho to use image files when building an executable

Postby 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.

#----------------------------------------------
#region Application Functions
#----------------------------------------------
 
#endregion Application Functions
 
#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-Demo-ButtonImages_psf {
 
#----------------------------------------------
#region Import the Assemblies
#----------------------------------------------
[void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
#endregion Import Assemblies
 
#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$button1 = New-Object 'System.Windows.Forms.Button'
$buttonOK = New-Object 'System.Windows.Forms.Button'
$imagelist1 = New-Object 'System.Windows.Forms.ImageList'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects
 
#----------------------------------------------
# User Generated Script
#----------------------------------------------
 
$form1_Load={
#TODO: Initialize Form Controls here
 
}
 
$button1_Click={
 
$button1.ImageIndex = if($button1.ImageIndex){0}else{1}
}
 
# --End User Generated Script--
#----------------------------------------------
#region Generated Events
#----------------------------------------------
 
$Form_StateCorrection_Load=
{
#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
 
$Form_Cleanup_FormClosed=
{
#Remove all event handlers from the controls
try
{
$button1.remove_Click($button1_Click)
$form1.remove_Load($form1_Load)
$form1.remove_Load($Form_StateCorrection_Load)
$form1.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch [Exception]
{ }
}
#endregion Generated Events
 
#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
$form1.SuspendLayout()
#
# form1
#
$form1.Controls.Add($button1)
$form1.Controls.Add($buttonOK)
$form1.AcceptButton = $buttonOK
$form1.ClientSize = '284, 262'
$form1.FormBorderStyle = 'FixedDialog'
$form1.MaximizeBox = $False
$form1.MinimizeBox = $False
$form1.Name = 'form1'
$form1.StartPosition = 'CenterScreen'
$form1.Text = 'Form'
$form1.add_Load($form1_Load)
#
# button1
#
$button1.ImageIndex = 0
$button1.ImageList = $imagelist1
$button1.Location = '105, 55'
$button1.Name = 'button1'
$button1.Size = '75, 75'
$button1.TabIndex = 1
$button1.TextImageRelation = 'ImageAboveText'
$button1.UseVisualStyleBackColor = $True
$button1.add_Click($button1_Click)
#
# buttonOK
#
$buttonOK.Anchor = 'Bottom, Right'
$buttonOK.DialogResult = 'OK'
$buttonOK.Location = '197, 227'
$buttonOK.Name = 'buttonOK'
$buttonOK.Size = '75, 23'
$buttonOK.TabIndex = 0
$buttonOK.Text = '&OK'
$buttonOK.UseVisualStyleBackColor = $True
#
# imagelist1
#
$Formatter_binaryFomatter = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
#region Binary Data
$System_IO_MemoryStream = New-Object System.IO.MemoryStream (,[byte[]][System.Convert]::FromBase64String('
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAu
MC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAA
ACZTeXN0ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkD
AAAADwMAAABADQAAAk1TRnQBSQFMAgEBAgEAAQgBAAEIAQABQAEAAUABAAT/AQkBAAj/AUIBTQE2
AQQGAAE2AQQCAAEoBAABAQIAAUADAAEBAQABCAYAAUAYAAGAAgABgAMAAoABAAGAAwABgAEAAYAB
AAKAAgADwAEAAcAB3AHAAQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgE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BGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAEBGQNqAAAFCAU0BPgcAAT4DAAEo
BAABAQIAAUADAAEBAQABAQYAAQgWAAP//wD/AP8A/wD/AP8A/wD/AAkACw=='
))
#endregion
$imagelist1.ImageStream = $Formatter_binaryFomatter.Deserialize($System_IO_MemoryStream)
$Formatter_binaryFomatter = $null
$System_IO_MemoryStream = $null
$imagelist1.TransparentColor = 'Transparent'
$form1.ResumeLayout()
#endregion Generated Form Code
 
#----------------------------------------------
 
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$form1.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $form1.ShowDialog()
 
} #End Function
 
#Call the form
Call-Demo-ButtonImages_psf | Out-Null
Attachments
Demo-ButtonImages.psf
(41.79 KiB) Downloaded 68 times
User avatar
monoeagle
Posts: 94
Joined: Tue Oct 13, 2015 9:10 am

Re: Ho to use image files when building an executable

Postby 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: 10030
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Ho to use image files when building an executable

Postby 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:

$button1_Click={
$button1.ImageIndex = if($button1.ImageIndex){0}else{1}
}

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: No registered users and 8 guests