Skip to content

Instantly share code, notes, and snippets.

@GoodOlClint
Created October 2, 2014 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GoodOlClint/80221f18c470b0f91208 to your computer and use it in GitHub Desktop.
Save GoodOlClint/80221f18c470b0f91208 to your computer and use it in GitHub Desktop.
Displays a random cat picture from TheCatApi
function Show-Cat
{
if($psversiontable.psversion.major -lt 3)
{
Write-Host 'Cats are only available on Powershell Version 3 or later'
return
}
[void][Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
$proxy = [System.Net.WebRequest]::GetSystemWebproxy().GetProxy('http://thecatapi.com/')
$settings = @{}
if($proxy.AbsoluteUri -ne 'http://thecatapi.com/')
{
$Settings['ProxyUseDefaultCredentials'] = $true
$Settings['Proxy'] = $proxy.AbsoluteUri
}
$request = Invoke-WebRequest -Uri 'http://thecatapi.com/api/images/get' -Method get @Settings
$webImg = Invoke-WebRequest -Uri $request.BaseResponse.ResponseUri.AbsoluteUri -Method get @Settings
$stream = New-Object System.IO.MemoryStream (,$webImg.Content)
$img = [System.Drawing.Image]::FromStream($stream)
$Form = New-Object Windows.Forms.Form
$Form.Text = 'Cats!'
$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() } )
[void]$Form.ShowDialog()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment