我有多个系统,每个系统都有不同的显示分辨率。最小显示分辨率为 1024x768,最大显示分辨率为 1920x1080。不知道如何让此代码根据显示分辨率自动调整。
[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()
}
#NUC bounds
$bounds = [Drawing.Rectangle]::FromLTRB(0, -1080, 1920, 1080)
$PC_name=$(Get-WmiObject Win32_Computersystem).name
$dateandtime = Get-Date -Format yyyy-MM-dd-hh-mm-ss
$path_pcname= "C:\Scripts\Screenshots\" + $PC_name + "_screenshot_" + "$dateandtime"+ ".png"
screenshot $bounds $path_pcname
$limit = (Get-Date).AddMinutes(-15)
$path_todelete = "C:\Scripts\Screenshots\"
# Delete files older than the $limit.
Get-ChildItem -Path $path_todelete -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
Start-Sleep -Seconds 10
答案1
您可以使用System.Windows.Forms.Screen
来获取分辨率数据。
$monitor = [System.Windows.Forms.Screen]::PrimaryScreen
[void]::$monitor.WorkingArea.Width
[void]::$monitor.WorkingArea.Height
并使用如下:
$bounds = [Drawing.Rectangle]::FromLTRB(0, -($monitor.WorkingArea.Height), $monitor.WorkingArea.Width, $monitor.WorkingArea.Height)
如果您有多个屏幕或者监视器设备不存在该属性,则可能会出现一些问题。