用 PowerShell 截取全屏并保存为 PNG

脚本化截图:抓取整个虚拟桌面(含多显示器)并写入带时间戳的 PNG,适合监控、无人值守任务和定时截图。

Windows截图开发媒体低风险
在 Windows PowerShell 中运行上面的 CopyFromScreen 代码,即可把整个桌面截图保存到桌面的 screen.png。

01命令

命令
$ Add-Type -AssemblyName System.Windows.Forms,System.Drawing; $b=[System.Windows.Forms.SystemInformation]::VirtualScreen; $bmp=New-Object System.Drawing.Bitmap $b.Width,$b.Height; $g=[System.Drawing.Graphics]::FromImage($bmp); $g.CopyFromScreen($b.Left,$b.Top,0,0,$bmp.Size); $bmp.Save("$env:USERPROFILE\Desktop\screen.png"); $g.Dispose(); $bmp.Dispose()

02作用说明

脚本先读取 VirtualScreen 边界(所有已连接显示器的外接矩形,因此多显示器会一并截入),创建同尺寸的 Bitmap,再用 Graphics.CopyFromScreen 把屏幕内容拷贝进去。定时截图时给保存路径加时间戳,如 `screen_$(Get-Date -Format yyyyMMdd_HHmmss).png`,配合任务计划程序即可自动运行。若只想截取单个窗口,可通过 Add-Type 调用 Win32 的 PrintWindow,或直接按 Win+Shift+S 使用截图工具。必须在交互式桌面会话中运行——锁屏或断开的 RDP 会话只会截到黑图。

03风险

风险低风险
使用条件Windows PowerShell 5.1; must run in an interactive session (a locked or disconnected RDP screen captures black).
!注意: 截图中可能包含屏幕上的密码和个人信息——定时截图脚本的输出位置要慎重选择。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • System.Drawing.Graphics class | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    依据: Microsoft documents the Graphics class created from an image via Graphics.FromImage, used as the drawing surface for CopyFromScreen screen captures.

最后验证: 2026-09-17 · 分类: 媒体

06相关方案