Show Image from Clipboard

Table of contents for Clipboard

  1. Show Image from Clipboard

This is first tutorial in series of “Clipboard” tutorials. I just show you fast way how to display image immediately after copying it to clipboard and how to delete it from Clipboard. Please be aware, that this works only in AIR, not in browser.

So first step, we should access clipboard, that’s very easy:

var cb:Clipboard = Clipboard.generalClipboard;

In second step we have to find out, if content in Clipboard is Bitmap. For this purposes clipboard has function hasFormat:

if (cb.hasFormat(ClipboardFormats.BITMAP_FORMAT)) {
var bd:BitmapData = cb.getData(ClipboardFormats.BITMAP_FORMAT) as BitmapData;
var ce:ClipboardEvent = new ClipboardEvent(ClipboardEvent.BITMAP_IN_CLIPBOARD, true);
ce.bitmapData = bd;
dispatchEvent(ce);
//clear image from clipboard
cb.clearData(ClipboardFormats.BITMAP_FORMAT);
}

As you can see, BitmapData was easily get from Clipboard by using function getData:

var bd:BitmapData = cb.getData(ClipboardFormats.BITMAP_FORMAT) as BitmapData;

Next I just dispatch BitmapData through my custom ClipboardEvent. If you want remove BitmapData from clipboard you can use:

cb.clearData(ClipboardFormats.BITMAP_FORMAT);

It removes just BitmapData not texts in Clipboard. And if you want display Bitmap from Clipboard immediately after pressing PrnScrn key on your keyboard, you have to start Timer for example each 100ms, because there is no Event (at least I don’t know about it) which tells you, that something was copied to clipboard.

If you want to see example application, please download my FacebookCheats AIR application, switch to Print Screen Tab and press PrnScrn key. :)

Leave a Reply