如何在PowerShell中捕获屏幕?我需要能够将屏幕保存到磁盘。
发布于 2010-06-04 06:47:42
您还可以使用.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"
发布于 2017-06-18 04:41:36
为了完整起见,此脚本允许您跨多个监视器截图。
基本代码来自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();
}
可以通过: screenshot "D:\screenshot.png“调用
发布于 2010-06-04 04:01:46
此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)
}
}
希望这能有所帮助
https://stackoverflow.com/questions/2969321
复制相似问题