我正在尝试将剪贴板中的图像保存到文件路径。我已经尝试了下面的脚本,但它返回了"剪贴板不包含图像数据"的错误信息。
由于Clipboard类只能在设置为单线程公寓(STA)模式的线程中使用。
我已经尝试在中运行脚本。
Add-Type -AssemblyName System.Windows.Forms
if ($([System.Windows.Forms.Clipboard]::ContainsImage())) {
$image = [System.Windows.Forms.Clipboard]::GetImage()
$filename='e:\test\test.png'
[System.Drawing.Bitmap]$image.Save($filename, [System.Drawing.Imaging.ImageFormat]::Png)
Write-Output "clipboard content saved as $filename"
} else {
Write-Output "clipboarsd does not contains image data"
}
由于Clipboard类只能在设置为单线程公寓(STA)模式的线程中使用。
我已经尝试在中运行脚本。
powershell -NoProfile -Sta -File $file
另外,我尝试重新启动如果运行空间不是STA,但这并没有帮助。
Add-Type -AssemblyName System.Windows.Forms
if ($host.Runspace.ApartmentState -ne "STA") {
"Relaunching"
$file = "./saveImage.ps1"
powershell -NoProfile -Sta -File $file
return
}