How can I do a screen capture in Windows PowerShell?

前端 未结 5 579
礼貌的吻别
礼貌的吻别 2020-11-28 05:51

How can I capture the screen in Windows PowerShell? I need to be able to save the screen to disk.

5条回答
  •  北海茫月
    2020-11-28 06:19

    Here is my solution for multi-monitor, which is a bit simpler than the current answer. It also will render screens properly if the user has a strange monitor config (stacked vertical, etc) without black bars.

    Add-Type -AssemblyName System.Windows.Forms,System.Drawing
    
    $screens = [Windows.Forms.Screen]::AllScreens
    
    $top    = ($screens.Bounds.Top    | Measure-Object -Minimum).Minimum
    $left   = ($screens.Bounds.Left   | Measure-Object -Minimum).Minimum
    $width  = ($screens.Bounds.Right  | Measure-Object -Maximum).Maximum
    $height = ($screens.Bounds.Bottom | Measure-Object -Maximum).Maximum
    
    $bounds   = [Drawing.Rectangle]::FromLTRB($left, $top, $width, $height)
    $bmp      = New-Object System.Drawing.Bitmap ([int]$bounds.width), ([int]$bounds.height)
    $graphics = [Drawing.Graphics]::FromImage($bmp)
    
    $graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
    
    $bmp.Save("$env:USERPROFILE\test.png")
    
    $graphics.Dispose()
    $bmp.Dispose()
    

提交回复
热议问题