Created
May 13, 2011 02:20
-
-
Save zippy1981/969855 to your computer and use it in GitHub Desktop.
Display an image from Windows Powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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(); |
Created 11 years ago but useful to me this morning! Thanks people.
Great to hear @jacquesfrancis
Thanks for this. Just used it!
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()
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()
}
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
LoadWithPartialName is deprecated. Use this instead: