小编Pro*_*tor的帖子

使用 PowerShell 从剪贴板保存图像

我正在尝试将图像从剪贴板保存到文件路径。我试过下面的脚本,它返回“剪贴板不包含图像数据”。

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"
}
Run Code Online (Sandbox Code Playgroud)

由于Clipboard该类只能用于设置为单线程单元 (STA) 模式的线程。

我试图在

powershell -NoProfile -Sta -File $file
Run Code Online (Sandbox Code Playgroud)

另外,如果运行空间不是 STA,我尝试重新启动,这没有帮助。

Add-Type -AssemblyName System.Windows.Forms
if ($host.Runspace.ApartmentState -ne "STA") {
    "Relaunching"
    $file = "./saveImage.ps1"
    powershell -NoProfile -Sta -File $file 
    return
}
Run Code Online (Sandbox Code Playgroud)

powershell powershell-4.0

5
推荐指数
1
解决办法
3074
查看次数

标签 统计

powershell ×1

powershell-4.0 ×1