Cross Domain Flash 8 BitmapData

March 15, 2006 | Uncategorized | 1 comment

This week, we added a feature to our online education player that creates small thumbnails of upcoming slides (see sample class video).

To do this, I used a new feature in Flash 8 that allows copying a MovieClip as a bitmap in three simple lines:

// copy from source_mc -> target_mc
var bitmap = new flash.display.BitmapData(source_mc._width, source_mc._height);
target_mc.attachBitmap(bitmap, 1);

The only problem is that if a SWFon domainA tries to copy a MovieClip loaded from domainB (source_mc) and source_mc has any content from another domain (domainB), BitmapData won't work. The online education player loads the slide images from different domain (domainB) than that which hosts the player (domainA), so initally the BitmapData code didn't work. If we were loading SWF files from domainB, we could add the following code to the domainA fla/swf:"domainA");

But this doesn't work for JPGs, PNGs, etc. since you can't add this code to an image file. Macromedia suggested putting all the images inside SWFs, but that would not be feasible since we have 1000s of slides. Based on some code and a few emails from
Frédéric v. Bochmann, we were able to get around this by creating a wrapper flash file that loads the images I wanted and then returns a BitmapData.

Instead of domainA.swf doing:

bitmap = new flash.display.BitmapData(container_mc._width, container_mc._height);

domainA now does this:

bitmap = container_mc.getBitmap();

Then in domainB/loader.swf, the code looks like:"domainA");
this.createEmptyMovieClip("container_mc", 1);
function getBitmap() {
    return new flash.display.BitmapData(container_mc._width, container_mc._height);

This is a simplified example, so you should add some code to check for the loaded state of the image01.jpg file (Frédéric v. Bochmann does this in his example). Also, when passing the filename=image01.jpg, you must use a fully qualified domain name (such as domainB/image01.jpg), or else the domainB.swf will try to load the file from domainA and Flash will throw error about cross-domain loading.

Try it out

One response to “Cross Domain Flash 8 BitmapData”

  1. felix says:

    This is cool, but only works if you have access to both domains. Sometimes you don’t have access to the domain you are loading images from.

Hi, I'm John Dyer. In my day job, I build websites and create online seminary software for a seminary in Dallas. I also like to release open source tools including a pretty popular HTML5 video player and build tools that help people find best bible commentaries and do bible study. And just for fun, I also wrote a book on the theology of technology and media.

Fork me on GitHub

Social Widgets powered by