Adding Pictures to Forms

hi,

when adding a picturebox and assign a picture to it, in the form designer it shows good (for the first time).
but, after saving the form, in the design it add the lines:

$resources = Invoke-Expression (Get-Content -Path (Join-Path $PSScriptRoot 'NessTest.resources.psd1') -Raw)
PictureBox_Logo.BackgroundImage = ([System.Drawing.Image]$resources.'PictureBox_Logo.BackgroundImage')

but the File NessTest.resource.psd1 (in this example) is not created, so there is no logo \ Picture when running the script.
but there is an error on the console:

Get-Content : Cannot find path 'E:\Ps1\AD-EXport\NessTest.resources.psd1' because it does not exist.
At E:\Ps1\AD-EXport\NessTest.designer.ps1:10 char:33
+ ... Expression (Get-Content -Path (Join-Path $PSScriptRoot 'NessTest.reso ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (E:\Ps1\AD-EXport\NessTest.resources.psd1:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
 
Invoke-Expression : Cannot bind argument to parameter 'Command' because it is null.
At E:\Ps1\AD-EXport\NessTest.designer.ps1:10 char:32
+ ... -Expression (Get-Content -Path (Join-Path $PSScriptRoot 'NessTest.res ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

please help.

what should i do for adding pictures?
how do i create the PSD1 file?

thanks in advanced.

Amir.

Hey Amir,

Are you using the form designer in VS Code or Visual Studio?

HI,

I am using VS CODE.

Ok. Thanks. I’ve reproduced the issue and will try to get a fix into the next build.

i got the new build.
it got worth.
i can’t package anything.

when trying to package from VSCode i get the following error:

please Help…

another issue:
when trying to update the form designer i get an error:
image

Hi Amir, I’m actively looking into this issue. I’ll keep you updated on a resolution.

what about adding Images to the forms?

when creating an imagelist:

#ImageList1
$ImageList1.ImageStream = ([System.Windows.Forms.ImageListStreamer]$resources.‘ImageList1.ImageStream’)
$ImageList1.TransparentColor = [System.Drawing.Color]::Transparent
$ImageList1.Images.SetKeyName([System.Int32]0,[System.String]‘Arrows-Left.ico’)
$ImageList1.Images.SetKeyName([System.Int32]1,[System.String]‘Arrows-Right.ico’)
$ImageList1.Images.SetKeyName([System.Int32]2,[System.String]‘Mobile-Phone-Create.ico’)

when running it says:

Get-Content : Cannot find path ‘E:\Ps1\AD\GroupDiff\GroupDifferences.resources.psd1’ because it does not exist.
At E:\Ps1\AD\GroupDiff\GroupDifferences.designer.ps1:13 char:33

  • … Expression (Get-Content -Path (Join-Path $PSScriptRoot 'GroupDifferen …
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (E:\Ps1\AD\Group…resources.psd1:String) [Get-Content], ItemNotFoundException
      + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Please help me to create the Resources.psd1 file.

thanks in advanced.
Amir.

1 Like

Hey Amir,

It looks like for some reason the resources file isn’t being created in VS Code. It works in Visual Studio so something isn’t hooked up quite right. Stay tuned for a fix.

1 Like

i tried to work with visual studio and powershell. it’s not easy.
The Resources.psd1 was created when adding pictures and icon.
but, when trying to create an imagelist for using at ListView the Resources file went empty and didn’t had nothing in it.
Please help.
Visual Studio Code is better.

Could you please upgrade to 5.3.1 to see if it solves your issue?

hi,
thanks for your quick solutions.

  1. it fixed the resources file - with the pictures.
  2. it didn’t fix the issue about the imagelist element.
    in the design it adds the element:

but in the resources file it does not add any picture\icons that related to the ImageList element.

thanks,
Amir.

Hey Amir,

It looks like the ImageList is not implemented correctly and I’m looking at getting the first. Hopefully, will have a resolution soon.

Adam

I find my self having to load icons and images for their files at run-time vs during design. I assign images from either the resource file or imageList during design but they do not show up at runtime. Is anyone else encountering this issue. I am using Visual Studio Community 2019 with PowerShell Pro Tools for Visual Studio 5.4.3 (according to the downloaded install file) but in Visual Studio Help About it indicates version 1.0.

Hi Adam, i’m running into similar issues with trying to create an imageList that is used for a treeView element in a visual studio form. The content of the Form1.resources.ps1 file is empty, but the Form1.resx has the binary data of the imageList, but it doesn’t seem there’s a reference to this resx file ($resrouces points to the Form1.resources.ps1 file). I started looking into this issue when I was trying out my form and received the following errors:

Exception calling “SetKeyName” with “2” argument(s): “Index was outside the bounds of the array.”
At C:\Users\adminjlc\Desktop\Form1.designer.ps1:223 char:1

  • $imageList1.Images.SetKeyName([System.Int32]0,[System.String]‘up.bmp’ …
  •   + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : IndexOutOfRangeException
    
    

Exception calling “SetKeyName” with “2” argument(s): “Index was outside the bounds of the array.”
At C:\Users\adminjlc\Desktop\Form1.designer.ps1:224 char:1

  • $imageList1.Images.SetKeyName([System.Int32]1,[System.String]'down.bm …
  •   + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : IndexOutOfRangeException
    
    

I did some google searching to see if there was any documentation to System.IO.MemoryStream since matzas posted part of his .resources.ps1 file, but i’m still figuring out the proper way of ingesting the byte data.

If its related to the empty Form1.resources.ps1 file, is there anything I can manually add into it to get the imageList to function? Any help would be appreciated.

Thanks!
Joshua

I figured out a workaround for this issue, I converted the images to base64, then converted from bytes to an in memory stream, and finally into a System.Drawing.Image to be added into empty imageList via the $Form1_Load action:

original source: https://www.alkanesolutions.co.uk/2013/04/19/embedding-base64-image-strings-inside-a-powershell-application/

$base64IconString = “base64_string_here”
$iconimageBytes = [Convert]::FromBase64String($base64IconString)
$ims = New-Object IO.MemoryStream($iconimageBytes, 0, $iconimageBytes.Length)
$ims.Write($iconimageBytes, 0, $iconimageBytes.Length);
$alkIcon = [System.Drawing.Image]::FromStream($ims, $true)
$image_name = [System.Drawing.Image]::FromStream($ims, $true)

$Form1_Load = {
$imageList1.Images.Add($image_name)
}

Hope this helps anyone else running into this, and needs a workaround.

1 Like

Hey Joshua, thanks for the above code… that is letting progress with adding images to a treeview I’m working on. Very keen for this to be resolved for Visual Studio.