-1

我正在尝试在 powershell 中使用我的应用程序注入文本

$MODIObj = New-Object -ComObject MODI.Document
$MODIObj.Create($filepath)

是否可以直接从剪贴板获取我的图像?我试过这个:

$MODIObj.Create([System.Windows.Forms.Clipboard]::GetImage())

但它不起作用。是否可以在不制作文件的情况下尝试类似的事情?

4

1 回答 1

1

根据MSDNCreate()MDI 或 TIF 文档需要带有路径或文件名的字符串参数,这意味着它不会接受System.Drawing.Image您从中获取的 -object GetImage()。作为一种解决方法,您可以将存储在剪贴板中的图像保存到临时文件并尝试加载它。前任。

#Get image from clipboard
Add-Type -AssemblyName System.Windows.Forms
$i = [System.Windows.Forms.Clipboard]::GetImage()

#Save image to a temp. file
$filepath = [System.IO.Path]::GetTempFileName()
$i.Save($filepath)

#Create MODI.Document from filepath
$MODIObj = New-Object -ComObject MODI.Document
$MODIObj.Create($filepath)

如果Create()抱怨文件名(缺少扩展名),则只需将其添加到临时文件路径:

$filepath = [System.IO.Path]::GetTempFileName() + ".tif"

您还可以在文件上按复制(例如文件资源管理器中的 ctrl+c)并检索该路径。例子:

#Get image from clipboard
Add-Type -AssemblyName System.Windows.Forms

#If clipboard contains image-object
if([System.Windows.Forms.Clipboard]::ContainsImage()) {

    #Get image from clipboard
    $i = [System.Windows.Forms.Clipboard]::GetImage()

    #Save image to a temp. file
    $filepath = [System.IO.Path]::GetTempFileName()
    $i.Save($filepath)

} elseif ([System.Windows.Forms.Clipboard]::ContainsFileDropList()) {
    #If a file (or files) are stored in the clipboard (you have pressed ctrl+c/ctrl+x on file/files)
    $files = [System.Windows.Forms.Clipboard]::GetFileDropList()

    #Only using first filepath for this demo.
    #If you need to support more files, use a foreach-loop to ex. create multiple MODI.documents or process one at a time
    $filepath = $files[0]
}

#If filepath is defined
if($filepath) {
    #Create MODI.Document from filepath
    $MODIObj = New-Object -ComObject MODI.Document
    $MODIObj.Create($filepath)
}
于 2016-05-21T10:10:42.843 回答