-1

I am trying to inject text from my app in powershell using

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

Is it possible to directly get my image from clipboard? I've tried this:

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

But it doesn't work. Is it possible to try something like that without making a file?

Frode F.
  • 52,376
  • 9
  • 98
  • 114
cterra
  • 55
  • 1
  • 10

1 Answers1

1

According to MSDN, Create() requires a string parameter with path or filename for a MDI or TIF-document, which means it won't accept the System.Drawing.Image-object you get from GetImage(). As a workaround you can save the image stored in the clipboard to a temp-file and try to load that. Ex.

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

If Create() complains about filename (missing extension), then just add it to the temp-filepath:

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

You could also press copy on a file (ex. ctrl+c in File Explorer) and retrieve that path. Example:

#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)
}
Frode F.
  • 52,376
  • 9
  • 98
  • 114