Created
November 4, 2024 19:07
-
-
Save SteveGilham/37ef080a303828dd5409f4d83bec29c6 to your computer and use it in GitHub Desktop.
Get and display a random image under a base folder
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
# supply the root of your image archive folder as a parameter (or edit this file and hard-code $archiveroot) | |
param([String]$archiveroot="$env:TEMP") | |
Add-Type -AssemblyName 'System.Windows.Forms' | |
Add-Type -AssemblyName 'System.Drawing.Common' | |
$path = dir -recurse "$($archiveroot)\*.*" | Get-random | % {$_.FullName} | |
write-host $path | |
try { | |
$file = (get-item $path) | |
$img = [System.Drawing.Image]::Fromfile((get-item $file)) | |
[System.Windows.Forms.Application]::EnableVisualStyles() | |
$form = new-object Windows.Forms.Form | |
$form.Text = "Image Viewer " + $path | |
$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() | Out-Null | |
$img.Dispose() | |
} catch { Start-process $file } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment