powershell 分别对每个屏幕进行截图

k10s72fa  于 2023-06-23  发布在  Shell
关注(0)|答案(1)|浏览(180)

我试着把我的每一个屏幕截图。
很不幸,我找了几个小时,什么也没找到。
以下是我的PowerShell脚本内容:

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("%path%\tmp\Screenshots\%1.png") 
$graphics.Dispose() 
$bmp.Dispose() 
%powershell% -ExecutionPolicy Bypass -File "%path%\librairies\sg.ps1"

这需要我所有屏幕的截图,但在一个文件中,我想检测屏幕的数量,并对每个屏幕进行截图,例如文件名为:Screen1.pngScreen2.png等...

z6psavjg

z6psavjg1#

循环遍历[Windows.Forms.Screen]::AllScreen返回的每个屏幕:

Add-Type -AssemblyName System.Windows.Forms,System.Drawing 

foreach ($screen in [Windows.Forms.Screen]::AllScreens) {
    $bounds   = $screen.Bounds
    try {
        $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) 
        $screenName = $screen.DeviceName -replace '^.*\\([^\\]+)$'
        $bmp.Save("$env:PATH\tmp\Screenshots\$screenName.png")
    }
    finally {
        $graphics.Dispose() 
        $bmp.Dispose() 
    }
}

相关问题