Skip to content

Instantly share code, notes, and snippets.

@eonarheim
Last active December 6, 2018 19:03
Show Gist options
  • Save eonarheim/c75fedbf21fa4c06d49817b2f3082799 to your computer and use it in GitHub Desktop.
Save eonarheim/c75fedbf21fa4c06d49817b2f3082799 to your computer and use it in GitHub Desktop.
Screen Shot with powershell
## Simple screen shot powershell script
[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
# Base path to save screen shots
$basePath = "$env:USERPROFILE\Desktop\ld41-screenshots\"
# Bounding rectangle of the 1st screen (can be found in the windows display properties)
# The primary screen always starts at (0, 0), then other screens have bounds relative to the primary screen
$bounds1 = [Drawing.Rectangle]::FromLTRB(0, 0, 3240, 2160)
# The second screen bounds, and in my example is positioned to the left of the primary screen in the windows display manager. So the left coordinate is negative.
$bounds2 = [Drawing.Rectangle]::FromLTRB(-1680, 0, 0, 1050)
function Get-ScreenShot([Drawing.Rectangle]$bounds, $path) {
$bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
$graphics = [Drawing.Graphics]::FromImage($bmp)
$graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
$bmp.Save($path)
$graphics.Dispose()
$bmp.Dispose()
}
while($true) {
$date = (Get-Date -Format "yyyyMMddHHmmss")
try {
Write-Host "Screen shot @ $date"
Get-ScreenShot -bounds $bounds1 -path "$basePath/screen1_$date.png"
Get-ScreenShot -bounds $bounds2 -path "$basePath/screen2_$date.png"
} catch {
# beep if the screen shots ever stop
[console]::beep(1500, 10000)
}
Start-Sleep -Seconds 60;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment