Home > Software design >  How do I assign an image to an existing $Picturebox1 from file (.jpg) in Powershell?
How do I assign an image to an existing $Picturebox1 from file (.jpg) in Powershell?

Time:09-27

Powershell: I have an existing Picturebox1 and need to load its image from c:\project\image.jpg when form1 loads.

I know how to do, for example,

$Label20.location = '73,97'

but have tried 1000's of examples from Google searches to load an image file into the picturebox without success.

Surely it can't be that hard.

CodePudding user response:

There are several examples everywhere of this procedure:

$file = (get-item 'c:\temp\capture.jpg')
$myimg = [System.Drawing.Image]::Fromfile($file);

#
#Region FormElements
$form = New-Object System.Windows.Forms.Form
$form.Size = '525,675'
$form.StartPosition = 'CenterScreen'
$form.MaximizeBox = $false
#
#
# Branding image for form
$Image = New-Object System.Windows.Forms.PictureBox
$Image.Width = $MyImg.Width
$Image.Height = $MyImg.Height
$Image.Location = '100,100'
$Image.Image = $MyImg
$Image.Anchor = 'Top,Right'
#
#
$Form.controls.add($Image)
#
# Show form
$form.ShowDialog() | Out-Null
$form.Dispose()
# End

CodePudding user response:

Just for background:

I created the GUI in PSScriptpad which creates a .ps1, .designer.ps1 and a .resources.ps1 as those familiar with it will know.

My problem was EDITING an existing Picturebox creation in the .designer.ps1 to load the image from file and not from the .resources.ps1 as it was.

The answer by Scepticalist guided me to a solution.

The answer was not to try and override .designer and .resources at runtime but to edit the .designer. and comment out reference to the picturebox in .resources just to make sure.

P.S. I have 17 years extensive experience in VBA but I am new to Powershell and the .Net environment and got absolutely hooked.

Thanks for a great forum! I search it all the time.

  • Related