Cross Domain Flash 8 BitmapData

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);
bitmap.draw(source_mc);
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:

System.security.allowDomain("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:

container_mc.loadMovie("domainB/image01.jpg");
bitmap = new flash.display.BitmapData(container_mc._width, container_mc._height);

domainA now does this:

container_mc.loadMovie("domainB/loader.swf?filename=image01.jpg");
bitmap = container_mc.getBitmap();

Then in domainB/loader.swf, the code looks like:

System.security.allowDomain("domainA");
this.createEmptyMovieClip("container_mc", 1);
this.container_mc.loadMovie(this.filename);
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

1 thought on “Cross Domain Flash 8 BitmapData

  1. 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.

Comments are closed.