How can I do a screen capture in Windows PowerShell?

前端 未结 5 627
礼貌的吻别
礼貌的吻别 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:35

    This PowerShell function will capture the screen in PowerShell and save it to an automatically numbered file. If the -OfWindow switch is used, then the current window will be captured.

    This works by using the built in PRINTSCREEN / CTRL-PRINTSCREEEN tricks, and it uses a bitmap encoder to save the file to disk.

    function Get-ScreenCapture
    {
        param(    
        [Switch]$OfWindow        
        )
    
    
        begin {
            Add-Type -AssemblyName System.Drawing
            $jpegCodec = [Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | 
                Where-Object { $_.FormatDescription -eq "JPEG" }
        }
        process {
            Start-Sleep -Milliseconds 250
            if ($OfWindow) {            
                [Windows.Forms.Sendkeys]::SendWait("%{PrtSc}")        
            } else {
                [Windows.Forms.Sendkeys]::SendWait("{PrtSc}")        
            }
            Start-Sleep -Milliseconds 250
            $bitmap = [Windows.Forms.Clipboard]::GetImage()    
            $ep = New-Object Drawing.Imaging.EncoderParameters  
            $ep.Param[0] = New-Object Drawing.Imaging.EncoderParameter ([System.Drawing.Imaging.Encoder]::Quality, [long]100)  
            $screenCapturePathBase = "$pwd\ScreenCapture"
            $c = 0
            while (Test-Path "${screenCapturePathBase}${c}.jpg") {
                $c++
            }
            $bitmap.Save("${screenCapturePathBase}${c}.jpg", $jpegCodec, $ep)
        }
    }
    

    Hope this helps

提交回复
热议问题