如何在Windows PowerShell中执行屏幕捕获?

Sta*_*ing 33 powershell

如何在Windows PowerShell中捕获屏幕?我需要能够将屏幕保存到磁盘.

Jer*_*emy 39

您还可以使用.NET以编程方式获取屏幕截图(这样可以更好地控制):

[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
function 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()
}

$bounds = [Drawing.Rectangle]::FromLTRB(0, 0, 1000, 900)
screenshot $bounds "C:\screenshot.png"
Run Code Online (Sandbox Code Playgroud)


Sta*_*ing 12

此PowerShell功能将捕获PowerShell中的屏幕并将其保存到自动编号的文件中.如果使用-OfWindow开关,则将捕获当前窗口.

这可以通过使用内置的PRINTSCREEN/CTRL-PRINTSCREEEN技巧来实现,它使用位图编码器将文件保存到磁盘.

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助


Ska*_*ami 10

为了完成,此脚本允许您跨多个监视器截取屏幕截图.

基本代码来自Jeremy

[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
function screenshot($path) 
{
    $width = 0;
    $height = 0;
    $workingAreaX = 0;
    $workingAreaY = 0;

    $screen = [System.Windows.Forms.Screen]::AllScreens;

    foreach ($item in $screen)
    {
        if($workingAreaX -gt $item.WorkingArea.X)
        {
            $workingAreaX = $item.WorkingArea.X;
        }

        if($workingAreaY -gt $item.WorkingArea.Y)
        {
            $workingAreaY = $item.WorkingArea.Y;
        }

        $width = $width + $item.Bounds.Width;

        if($item.Bounds.Height -gt $height)
        {
            $height = $item.Bounds.Height;
        }
    }

    $bounds = [Drawing.Rectangle]::FromLTRB($workingAreaX, $workingAreaY, $width, $height); 
    $bmp = New-Object Drawing.Bitmap $width, $height;
    $graphics = [Drawing.Graphics]::FromImage($bmp);

    $graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size);

    $bmp.Save($path);

    $graphics.Dispose();
    $bmp.Dispose();
}
Run Code Online (Sandbox Code Playgroud)

可以调用:screenshot"D:\ screenshot.png"


mon*_*nny 6

Microsoft在此处提供了Powershell脚本:

http://gallery.technet.microsoft.com/scriptcenter/eeff544a-f690-4f6b-a586-11eea6fc5eb8

我刚刚在Windows 7机器上试过它并使用命令行示例提供它:

Take-ScreenShot -screen -file "C:\image.png" -imagetype png 
Run Code Online (Sandbox Code Playgroud)

  • 该链接(实际上)已损坏。它重定向到***非特定*** URL“https://learn.microsoft.com/en-us/samples/browse/?redirectedfrom=TechNet-Gallery”。 (3认同)
  • 打开...产生没有结果.在控制台中输入然后调用它也不会产生任何输入.这是Windows 10,这个线程已经存在多年而没有产生输入. (2认同)

Jac*_*vin 6

这是我的多显示器解决方案,比当前的答案简单一些。如果用户有一个奇怪的监视器配置(垂直堆叠等)而没有黑条,它也会正确呈现屏幕。

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()
Run Code Online (Sandbox Code Playgroud)