Skip to content

Instantly share code, notes, and snippets.

@zippy1981
Created May 13, 2011 02:20
Show Gist options
  • 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!

@danielbrownridge
Copy link

Update example tested on Powershell version 5.1 on Windows 11

using namespace System.Windows.Forms
using namespace System.Drawing

# Assume file called 'image.png' in the users 'Pictures' folder
$Title = "Display image with PowerShell example"
$ImageFileName = "image.png"

$PicturesPath = Join-Path -Path $env:USERPROFILE -ChildPath Pictures
$ImagePath = Join-Path -Path $PicturesPath -ChildPath $ImageFileName
$ImageFileInfo = Get-Item -Path $ImagePath
$Image = [Image]::FromFile($ImageFileInfo)

$PictureBox = [PictureBox]::new()
$PictureBox.Size = $Image.Size
$PictureBox.Image = $Image

$Form = [Form]::new()
$Form.Text = $Title
$Form.Size = $Image.Size
$Form.Controls.Add($PictureBox)
$Form.ShowDialog()

@ThomasETN
Copy link

ThomasETN commented Mar 10, 2025

Modified as a function for Powershell 7, tested version 7.5.0 on Windows 11:

function Show-Image($ImagePath){
	Add-Type -AssemblyName System.Windows.Forms
	
	$ImageFileInfo = Get-Item -Path $ImagePath
	$Image = [Drawing.Image]::FromFile($ImageFileInfo)

	$PictureBox = [Windows.Forms.PictureBox]::new()
	$PictureBox.Size = $Image.Size
	$PictureBox.Image = $Image

	$Form = [Windows.Forms.Form]::new()
	$Form.Size = $Image.Size
	$Form.Controls.Add($PictureBox)
	$Form.ShowDialog()
}

@danielbrownridge
Copy link

Modified as a function for Powershell 7, tested version 7.5.0 on Windows 11:

function Show-Image($ImagePath){
	Add-Type -AssemblyName System.Windows.Forms
	
	$ImageFileInfo = Get-Item -Path $ImagePath
	$Image = [Drawing.Image]::FromFile($ImageFileInfo)

	$PictureBox = [Windows.Forms.PictureBox]::new()
	$PictureBox.Size = $Image.Size
	$PictureBox.Image = $Image

	$Form = [Windows.Forms.Form]::new()
	$Form.Size = $Image.Size
	$Form.Controls.Add($PictureBox)
	$Form.ShowDialog()
}

Tidy 👍

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