Search code examples
actionscript-3apache-flexactionscriptairflex4

Getting a bitmap of a spark window


I have an AIR application that I'm working on in which I would like to basically get a bitmap of what's going on in a separate spark window. The use case is a scaled preview of the spark window that will likely be on a projector to the main display. I want to pump the bitmap into a spark image as a source. Googling this doesn't seem to reveal much or I just don't what terms to google. Can anybody point me in the right direction? Anybody have a better way to accomplish this?

Thanks!


Solution

  • If you would like to make a bitmap of some DisplayObject, be it the whole window (stage) or just a Sprite, you should use the BitmapData's draw() method.

    The following code will take a "screen shoot" of the whole stage, make a bitmap image of it and add it scaled to the top left corner:

    var bd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
    bd.draw(stage);
    
    var bitmap:Bitmap = new Bitmap(bd);
    bitmap.width = 300;
    bitmap.scaleY = bitmap.scaleX;
    addChild(bitmap);
    

    It would benefit you to read more on Bitmap and BitmapData to utilize such features as:

    • Pixel snapping
    • Smoothing
    • Transparency

    and others. For example smoothing is something that would make the image look better when scaled, but it counts as a filter and can be a performance hitter. That's why it would be better to apply smoothing on the bitmapdata when drawing (drawing scaled image is done with the matrix), not on a bitmap; but only when you don't plan to scale the image during runtime.

    Hope that answers your question!