1

I've looked everywhere but only found solutions revolving online images. I'm dealing with a local image with this directory: C:\Users\ME\Desktop\Venice.jpg

I need some Javascript or vbScript to simply set that image itself (not the path) into the clipboard, so that I can paste the image into an application like Word or MS Paint.

I've found the following code, but don't know how to adapt it for my use. There is no internet access required. A simple, local javascript function is all I need. I have no applicable html so I don't see what to substitute "div" with below. Any help is great!! Thank you.

var div = document.getElementById('DivtoCopy');
div.contentEditable = 'true';
var controlRange;
if (document.body.createControlRange) {
controlRange = document.body.createControlRange();
controlRange.addElement(div);
controlRange.execCommand('Copy');
}
div.contentEditable = 'false';
Mathomatic
  • 899
  • 1
  • 13
  • 38

1 Answers1

2

In PowerShell copying an image from a file to the clipboard would be as simple as this:

Add-Type -Assembly System.Windows.Forms
Add-Type -Assembly System.Drawing

$imgpath = 'C:\Users\ME\Desktop\Venice.jpg'
$img = [Drawing.Image]::FromFile($imgpath)
[Windows.Forms.Clipboard]::SetImage($img)
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thank you, I will install PowerShell soon and report back. – Mathomatic Oct 10 '15 at 15:40
  • I tried the code you provided but it gave me the following error: http://i.imgur.com/GifZsE3.png – Mathomatic Oct 10 '15 at 20:11
  • The following link seems to explain a solution but I don't know PowerShell at all. Any help? Thanks http://blogs.msdn.com/b/smondal/archive/2010/09/08/current-thread-must-be-set-to-single-thread-apartment-sta-mode-before-ole-calls-can-be-made.aspx – Mathomatic Oct 10 '15 at 20:33
  • @Hurmle You seem to be using PowerShell v2, which uses MTA by default (see [this answer](http://stackoverflow.com/a/127240/1630171) for an explanation what STA and MTA are). Either run `powershell.exe -STA` to enforce STA mode, or [upgrade to PowerShell v3](http://blogs.technet.com/b/heyscriptingguy/archive/2013/06/02/weekend-scripter-install-powershell-3-0-on-windows-7.aspx), which defaults to STA. I'd recommend the latter. If your OS doesn't support at least PowerShell v3 it's already out of support and you should upgrade immediately. – Ansgar Wiechers Oct 10 '15 at 22:06
  • It worked! Thank you for your continued support and helpful advice. Very much appreciated. Take care. – Mathomatic Oct 10 '15 at 23:00