问题描述
我每天拍摄 100 个打印屏幕图像并将它们粘贴到另外 100 个 MS 绘画文件中。我需要自动执行一次保存所有 100 个打开文件的过程。我知道使用 'tasklist | find "mspaint"' 将带来带有进程 ID 的所有任务,但如何添加更多步骤以使其全部保存?例如1.png,2.png...100.png。
解决方法
完全忘记 mspaint
- 使用 Windows PowerShell 的 Get-ClipBoard
命令获取屏幕截图并以编程方式将其写入磁盘:
<# use PrintScreen or Snipping Tool to capture the screen and copy to clipboard as normal #>
# then grab from clipboard
$screenshot = Get-Clipboard -Format Image
# then save to disk
$screenshot.Save("C:\path\to\screenshot.png",[System.Drawing.Imaging.ImageFormat]::Png)
# and free it from memory
$screenshot.Dispose()
,
感谢这篇帖子 https://www.timsblog.nl/2016/01/13/asynchronously-save-images-in-the-clipboard-to-file/,我终于找到了我问题的完美答案。整个过程可以使用 powershell_ise 完全自动化。
Register-EngineEvent -SourceIdentifier PowerShell.OnIdle -Action {
$content = Get-Clipboard -Format Image
If ($content)
{
$path = $psISE.CurrentFile.FullPath | Split-Path
$filename = "$path\ImageCap - " + (Get-date -Format 'hh-mm-ss') + '.png'
$content.Save($filename,'png')
Set-Clipboard -Value $Null
}
}