Skip to content

Instantly share code, notes, and snippets.

@zippy1981
Created May 13, 2011 02:20
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save zippy1981/969855 to your computer and use it in GitHub Desktop.
Save zippy1981/969855 to your computer and use it in GitHub Desktop.
Display an image from Windows Powershell
# Loosely based on http://www.vistax64.com/powershell/202216-display-image-powershell.html
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file = (get-item 'C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg')
#$file = (get-item "c:\image.jpg")
$img = [System.Drawing.Image]::Fromfile($file);
# This tip from http://stackoverflow.com/questions/3358372/windows-forms-look-different-in-powershell-and-powershell-ise-why/3359274#3359274
[System.Windows.Forms.Application]::EnableVisualStyles();
$form = new-object Windows.Forms.Form
$form.Text = "Image Viewer"
$form.Width = $img.Size.Width;
$form.Height = $img.Size.Height;
$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()
#$form.Show();
@r-pufky
Copy link

r-pufky commented Oct 31, 2020

LoadWithPartialName is deprecated. Use this instead:

Add-Type -AssemblyName 'System.Windows.Forms'
$file = (get-item 'C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg')
$img = [System.Drawing.Image]::Fromfile((get-item $file))

[System.Windows.Forms.Application]::EnableVisualStyles()
$form = new-object Windows.Forms.Form
...

@jacquesfrancis
Copy link

Created 11 years ago but useful to me this morning! Thanks people.

@zippy1981
Copy link
Author

Great to hear @jacquesfrancis

@AgentChronicles
Copy link

Thanks for this. Just used it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment