【问题标题】:Clipboard image to OCR MODI.Document剪贴板图像到 OCR MODI.Document
【发布时间】:2016-05-20 23:25:05
【问题描述】:

我正在尝试使用

在 powershell 中从我的应用程序中注入文本
$MODIObj = New-Object -ComObject MODI.Document
$MODIObj.Create($filepath)

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

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

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

【问题讨论】:

  • 您是否先加载了 winforms 程序集? (Add-Type -Assembly System.Windows.Forms)

标签: powershell ocr modi


【解决方案1】:

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

#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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2011-04-19
    相关资源
    最近更新 更多