PowerShell 技能连载 - 截屏

借助 System.Windows.Forms 中的类型,PowerShell 可以轻松捕获屏幕并将屏幕截图保存到文件中。下面的代码捕获整个虚拟屏幕,将屏幕截图保存到文件中,然后在相关程序中打开位图文件(如果有):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$Path = "$Env:temp\screenshot.bmp"
Add-Type -AssemblyName System.Windows.Forms

$screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
$width = $screen.Width
$height = $screen.Height
$left = $screen.Left
$top = $screen.Top
$bitmap = [System.Drawing.Bitmap]::new($width, $height)
$MyDrawing = [System.Drawing.Graphics]::FromImage($bitmap)
$MyDrawing.CopyFromScreen($left, $top, 0, 0, $bitmap.Size)

$bitmap.Save($Path)
Start-Process -FilePath $Path
作者

吴波

发布于

2021-09-30

更新于

2022-07-06

许可协议

评论