Trying to display a gif in powershell

Viewed 1455

I'm using the following to show the gif but it stops the rest of the script running.

If i change to $form.show() it runs the rest of the script but the gif dosent work

Any ideas?

   [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")

$file = (get-item "666.gif")

$img = [System.Drawing.Image]::Fromfile($file);

[System.Windows.Forms.Application]::EnableVisualStyles();
$form = new-object Windows.Forms.Form
$form.Width =  $img.Size.Width;
$form.Height =  $img.Size.Height;
$form.TopMost = $false
$form.FormBorderStyle = $none;
$Form.ControlBox = $False
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width =  $img.Size.Width;
$pictureBox.Height =  $img.Size.Height;
$pictureBox.Image = $img;
$form.controls.add($pictureBox)
$form.Add_Shown( { $form.Activate() } )
$form.ShowDialog()
2 Answers

By running the script in another runspace, you will get the behavior you expect.

# Create runspace
$runspace = [RunspaceFactory]::CreateRunspace()
$runspace.Open()

# Add script and run asynchronously
$runspace.CreatePipeline{

    Add-Type -AssemblyName System.Windows.Forms

    $image = [Drawing.Bitmap]"C:\sample.gif"

    $pictureBox = [Windows.Forms.PictureBox]@{ Dock = "Fill"; Image = $image }
    $form = [Windows.Forms.Form]@{ Size = $image.Size; FormBorderStyle = "None" }
    $form.Controls.Add($pictureBox)

    $form.ShowDialog()

}.InvokeAsync()

Use Show and set correctly the borderstyle property.

Try this:

[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")

$img = [System.Drawing.Image]::Fromfile("666.gif");

[System.Windows.Forms.Application]::EnableVisualStyles();
$form = new-object Windows.Forms.Form
$form.Width =  $img.Size.Width;
$form.Height =  $img.Size.Height;
$form.TopMost = $false
$form.FormBorderStyle = [System.Windows.Forms.BorderStyle]::None;
$Form.ControlBox = $False
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width =  $img.Size.Width;
$pictureBox.Height =  $img.Size.Height;
$pictureBox.Image = $img;
$form.controls.add($pictureBox)
$form.Add_Shown( { $form.Activate() } )
$form.Show()

1..20 | %{"Continue program {0}" -f $_}
Related