Created
October 2, 2014 01:40
-
-
Save GoodOlClint/80221f18c470b0f91208 to your computer and use it in GitHub Desktop.
Displays a random cat picture from TheCatApi
This file contains 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
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