Copy a Flash SWF Image to the Clipboard Using Javascript

Welcome to part three of this tutorial on getting an image from Flash into the user's clipboard. In the first two parts we took a screenshot of the Flash movie, passed it to the browser, and used Javascript (and a PHP call, for Internet Explorer) to embed the image in the webpage. Now we'll go ahead with the final step of adding that image to the clipboard. Here's a demo:

* sorry, only works in Internet Explorer. What about Firefox?

Copying an image to the clipboard in Internet Explorer

For IE, putting an image on the clipboard isn't much harder than copying text. You simple createa control range that includes the <img> tag and execute the "Copy" command. I tried to use the same method on an Image object that hadn't been added to the DOM tree but that didn't work.

// imageElem is the <img> object (Image)
var ctrlRange = document.body.createControlRange();
ctrlRange.add(imageElem);
ctrlRange.execCommand('Copy');

Now, since the image is being copied to the clipboard, you probably don't want to show it, so we'll set the width and height to zero. I tried using display:none but then nothing got copied to the clipboard. So:

<img id="imageContainer" style="width:0;height:0" />

We also have to wait until the image has loaded before we can copy it to the clipboard so we'll wait for the image's onload method to be called before executing the copy.

function copyImageContainerToClipboard()
{
	var imageElem = getElem('imageContainer');
	var ctrlRange = document.body.createControlRange();
	ctrlRange.add(imageElem);
	ctrlRange.execCommand('Copy');
}
 
var screenshotBase64 = flashMovie.getScreenshot();
 
var imageContainer = getElem('imageContainer');
 
imageContainer.onload = copyImageContainerToClipboard;
 
// the getImageSrc method was explained in the previous post.
imageContainer.src = getImageSrc(
	"data:image/png;base64," + screenshotBase64);

There you go. To sum up the process...

  1. Javascript calls the Flash method, getScreenshot, through the ExternalInterface
  2. getScreenshot fetches the bitmap data, encodes it as a JPEG, encodes that in Base64, and returns it to the Javascript caller.
  3. Depending on the browser, Javascript displays the image using either the data: URI scheme or a call to a server-side script. Either way, the image is displayed in an <img> tag.
  4. In IE, an onload handler adds the image to the clipboard using a control range.

What about Firefox?

Having free access to the user's clipboard is actually a bit of a security risk. Reading from the clipboard, you might be able to acquire sensitive information and the user would be none the wiser. Writing to the clipboard isn't so dangerous, but could be annoying if a webpage wrote over whatever data you had stored there.

As of IE7, there's a security notification and a prompt to allow access to the clipboard. Firefox does the same sort of thing, asking the user whether to allow the rather vague action, "Run or install software on your machine." However, copying text to the clipboard in either browser is certainly possible, if the user will allow it.

The problem with copying image data to Firefox is that it's so complicated! I'm sure it can be done but I don't know enough about the fine details of mozilla's API. Fortunately for me, Internet Explorer is the target machine for the project I'm working on and getting the clipboard functionality in Firefox isn't a priority. That said, I did some research on the subject because I wanted a general solution and I'm a Firefox user myself. If you feel like taking up the problem on your own, there are some links below to get you started. Be sure to let me know if you make some headway.

If I get more info on the topic I'll post it here for all of your automatically​-copying​-an​-image​-to​-the​-clipboard needs. (-;

Comments

Thauwa says, "Can someone post an example please?"
Thauwa's picture

This is confusing me........
I need a simple example with codes to study...
Someone please help me!

By the way... Thanks Mr. Daniel!
This is the only explanation in the entire web on how to copy flash swf movie snapshots to clipboard..
But unfortunately...I dont seem to understand it. :(

Thauwa says, "OK...I go straight....."
Thauwa's picture

I am a kid trying to learn...I tried all the possibilities I knew..

Can anyone please make the flash movie in the link that is given with the facility of above "demo".
i.e. COPY TO CLIPBOARD

I cant pay anyone because I am a child....but I will certainly put my saviour's website 'URL or anything' on my sites.

I really need that facility...please post the codes......

Heres the link : http://fernandotharindu0.googlepages.com/flash-sketchpad-example.swf

I just made it in a hurry...

Here is an EMBEDD CODE for the above movie.

http://thauwa.googlepages.com/embeddcode

Thank you.....

Thauwa says, "Sorry I posted the thing twice......."
Thauwa's picture

I just double clicked..

Jaibabu says, "Hello Daniel, Thanks for"
Jaibabu's picture

Hello Daniel,

Thanks for this code. I implemented this in my app. But i want it to work in Firefox also. I had googled but everything end up negatively. Can anyone figured out how to copy the image to the clipboard using javascript in firefox?

Thanks
Jai

darvenginzks says, "Hello I'm new here"
darvenginzks's picture

I've recently joined and wanted to introduce myself :)

Rakesh Oswal says, "Copy to Clipboard without using server-side script"
Rakesh Oswal's picture

Hi Daniel,

In your example, after getting data in the form of Base64, your sending it to server and converting that data into PNG file and sending it back to client side. I want to do the same thing but without using server-side scripting. Or there is any other where I can create the file runtime on client machine. Within my application I am using javascript and flash CS3 and I don't want to use Adobe AIR.

Thanks,
Rakesh

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a Gravatar account, used to display your avatar.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

About

Daniel McLaren

Daniel is a Flash and Flex developer specializing in the art of information visualization.

Latest from SketchyD

Latest Drawing from SketchyD

This is the most recent drawing from my mobile sketch blog, SketchyD.com.

Recent comments