How can I do a screen capture in Windows PowerShell?

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

    For the sake of completion, this script allows you to take screenshots across multiple monitors.

    Base code comes from 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();
    }
    

    Can be called with: screenshot "D:\screenshot.png"

提交回复
热议问题