PoshCode Archive  Artifact [4f2137a147]

Artifact 4f2137a1477c499025ebbadea8e0e3be00f6a4d0256dcf6b3a41d72c3116173f:

  • File Export-ScreenShot.ps1 — part of check-in [cfa5d6d3ae] at 2018-06-10 14:02:39 on branch trunk — Export-Screenshot and Get-Screenshot let you get a System.Drawing.Bitmap of the full virtual screen or a cropped area of it, and save it to file … (user: Joel Bennett size: 1933)

# encoding: ascii
# api: powershell
# title: Export-ScreenShot
# description: Export-Screenshot and Get-Screenshot let you get a System.Drawing.Bitmap of the full virtual screen or a cropped area of it, and save it to file …
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Get-Screenshot
# x-poshcode-id: 585
# x-archived: 2014-08-14T23:34:01
# x-published: 2009-09-15T02:18:00
#
#
## Export-Screenshot to take a screenshot and save it to disk
#####################################################################################
## Usage:
##   Export-Screenshot sshot.png
##   Export-Screenshot screen.jpg (New-Object Drawing.Rectangle 0, 0, 640, 480)
#####################################################################################


$null = [Reflection.Assembly]::LoadWithPartialName( "System.Windows.Forms" )

# Get a screenshot as a bitmap      
function Get-Screenshot ([Drawing.Rectangle]$bounds) {
   $screenshot = new-object Drawing.Bitmap $bounds.width, $bounds.height
   $graphics = [Drawing.Graphics]::FromImage($screenshot)
   # $bounds.Location.Offset(50,50)
   $graphics.CopyFromScreen( $bounds.Location, [Drawing.Point]::Empty, $bounds.size)
	$graphics.Dispose()
   return $screenshot
}

# Save a screenshot to file
function Export-Screenshot {
PARAM (
   [string]$FilePath, 
   [Drawing.Rectangle]$bounds = [Windows.Forms.SystemInformation]::VirtualScreen
)
   write-host "FilePath: $($FilePath)" -fore green

   # Fix paths, in case they don't set [Environment]::CurrentDirectory
   if(!(split-path $FilePath).Length) { 
      $FilePath = join-path $pwd (split-path $FilePath -leaf)
      Write-Host "FilePath: $($FilePath)" -fore magenta
   }
   
   write-host "FilePath: $($FilePath)" -fore cyan

   $screenshot = Get-Screenshot $bounds
   $screenshot.Save( $($FilePath) )
   $screenshot.Dispose()
   gci $FilePath
}