Search code examples
formspowershellpicturebox

powershell form picture box function not working


I have the following code that I can run outside of a function and it works just fine. I am trying to make a form that will be displaying multiple images so I need to format it into a function.

Add-Type -AssemblyName System.Windows.Forms, System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                                 = New-Object system.Windows.Forms.Form
$Form.StartPosition                   = "CenterScreen"
$Form.Size                            = New-Object System.Drawing.Point(800,800)
$Form.text                            = "Example Form"
$Form.TopMost                         = $false
$Form.MaximumSize                     = $Form.Size
$Form.MinimumSize                     = $Form.Size

$Picture = (get-item ("C:\Users\User\Desktop\t.png"))
$img = [System.Drawing.Image]::Fromfile($Picture)
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width =  $img.Size.Width
$pictureBox.Height =  $img.Size.Height
$pictureBox.Image = $img
$pictureBox.Location  = [Drawing.Point]::new(15, 15)
$Form.controls.add($pictureBox)


$Form.ShowDialog()

but when I tried to assemble this as a function it is not working

Add-Type -AssemblyName System.Windows.Forms, System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                                 = New-Object system.Windows.Forms.Form
$Form.StartPosition                   = "CenterScreen"
$Form.Size                            = New-Object System.Drawing.Point(800,800)
$Form.text                            = "Example Form"
$Form.TopMost                         = $false
$Form.MaximumSize                     = $Form.Size
$Form.MinimumSize                     = $Form.Size

function pictureBox {
    [CmdletBinding()]
    param (
        [Parameter (Mandatory = $True)]
        [string] $p,

        [Parameter (Mandatory = $True)]
        [int] $lx,

        [Parameter (Mandatory = $True)]
        [int] $ly

    )
        $Picture              = (get-item ($p))
        $img                  = [System.Drawing.Image]::Fromfile($Picture)
        $pictureBox           = new-object Windows.Forms.PictureBox
        $pictureBox.Size      = [Drawing.Size]::new($img.Size.Width,$img.Size.Height)
        $pictureBox.Location  = [Drawing.Point]::new($lx, $ly)
        $pictureBox.Image     = $img
        
    }

$v1 = pictureBox -p "C:\Users\User\Desktop\t.png" -lx 15 -ly 15

$Form.controls.add($v1)

$Form.ShowDialog()

What am I doing wrong?


Solution

  • Your function pictureBox is not returning anything, it creates a new PictureBox instance, updates it's properties but then that object is never outputted, hence $v1 gets assigned AutomationNull.Value.

    # Same code for creating the Form here
    
    function pictureBox {
        [CmdletBinding()]
        param (
            [Parameter (Mandatory = $True)]
            [string] $p,
    
            [Parameter (Mandatory = $True)]
            [int] $lx,
    
            [Parameter (Mandatory = $True)]
            [int] $ly
        )
    
        $img = [Drawing.Image]::Fromfile($PSCmdlet.GetUnresolvedProviderPathFromPSPath($p))
        [Windows.Forms.PictureBox]@{
            Size      = [Drawing.Size]::new($img.Size.Width, $img.Size.Height)
            Location  = [Drawing.Point]::new($lx, $ly)
            Image     = $img
        }
    }
    
    $v1 = pictureBox -p "C:\Users\User\Desktop\t.png" -lx 15 -ly 15
    $Form.controls.add($v1)
    $Form.ShowDialog()