Skip to content

Instantly share code, notes, and snippets.

@vincent1890
Forked from Santaro255/take_screenshot.ps1
Created May 28, 2024 18:30
Show Gist options
  • Save vincent1890/2ef2971658b0412da06bcad6a9edc385 to your computer and use it in GitHub Desktop.
Save vincent1890/2ef2971658b0412da06bcad6a9edc385 to your computer and use it in GitHub Desktop.
multiple monitors
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.SystemParameters")
$bounds = [System.Windows.Forms.Screen]::PrimaryScreen
$w = $bounds.bounds.width#[System.Windows.SystemParameters]::VirtualScreenWidth
$h = $bounds.bounds.height#[System.Windows.SystemParameters]::VirtualScreenHeight
$img = New-Object System.Drawing.Bitmap($w, $h, [System.Drawing.Imaging.PixelFormat]::Format32bppArgb)
$cap = [System.Drawing.Graphics]::FromImage($img)
$cap.CopyFromScreen(0, 0, $bounds.Bounds.Size)
$img.Save("$env:USERPROFILE\screenshot.png", [System.Drawing.Imaging.ImageFormat]::Png)
#========================================================================================
<#
#get desktop width
$w = [System.Windows.SystemParameters]::VirtualScreenWidth
#get desktop height
$h = [System.Windows.SystemParameters]::VirtualScreenHeight
#construct bitmap (32bit RGBA)
$img = New-Object System.Drawing.Bitmap($w, $h, [System.Drawing.Imaging.PixelFormat]::Format32bppArgb)
#create graphics image from bitmap
$cap = [System.Drawing.Graphics]::FromImage($img)
#capture screen
$cap.CopyFromScreen(0, 0, [System.Drawing.Size]::new($w, $h))
#save captured image in png format
$img.Save("C:\screenshot.png", [System.Drawing.Imaging.ImageFormat]::Png)
#>
<# old
$bounds = [System.Windows.Forms.Screen]::PrimaryScreen
$w = [System.Windows.SystemParameters]::VirtualScreenWidth
$h = [System.Windows.SystemParameters]::VirtualScreenHeight
$img = New-Object System.Drawing.Bitmap($w, $h, [System.Drawing.Imaging.PixelFormat]::Format32bppArgb)
$cap = [System.Drawing.Graphics]::FromImage($img)
$cap.CopyFromScreen(0, 0, $bounds.Bounds.Size)
$img.Save("C:\test.png", [System.Drawing.Imaging.ImageFormat]::Png)
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment