Wednesday, September 16, 2015

Can you create a Windows Form with PowerShell and embed an image?

You can do this create in PowerShell. Create the new FORM and new  PICTUREBOX objects using the New-Object syntax.

Let us take a image flower.jpg (320x240) and place this image in a PictueBox control on a Windows Form.
PictureBoxe's top, left, width and height can be set. Similarly form's width and height and the title's text can be set.

Here is the complete code:
$image = [System.Drawing.Image]::Fromfile('C:\Users\mysorian\Desktop\flower.png')    
$pictureBox = new-object Windows.Forms.PictureBox  --instantiates a PictureBox $pictureBox.width=320

 $Form = New-Object Windows.Forms.Form   --instantiates a new form $Form.Text = "PowerShell Form"                     --sets the text for the form $Form.Width = 600
 $Form.Height = 600
 $form.Controls.add($pictureBox)                   --adds the PictureBox to the form $Form.Add_Shown({$Form.Activate()})

This should bring up this form with the image:


No comments: