Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Convert DisplayObject to Bitmap

with one comment

This API will return a Bitmap Image of any DisplayObject in Flash AS3.0. This was developed by Avijit Dutta, one of my friends. Hope this helps someone out there:

/**
*  @usage : var bitmap:Bitmap = BitmapUtil.getImage( source, new Rectangle( 0, 0, 100, 100 ) );
*  @params: source - DisplayObject whose Bitmap is required
*                   area - the part of the Object to be converted to an image.
*                    If the whole object is required to be converted then set null.
*/
public static function getImage( source:DisplayObject, area:Rectangle = null ):Bitmap
{
     var bitmapData:BitmapData;
     var matrix:Matrix;

     if ( area != null ) {
           matrix = new Matrix( 1, 0, 0, 1 , Math.abs( area.x ) , Math.abs( area.y ) );

           if ( area.width > 0 && area.height > 0 )
                bitmapData = new BitmapData( area.width, area.height, true, 0x0 );
           else
                bitmapData = new BitmapData( source.width, source.height, true, 0x0 );
     } else {

           bitmapData = new BitmapData( source.width , source.height , true, 0x0 );
     }

     bitmapData.draw( source, matrix, null, null, area, true );

     var bitmap:Bitmap = new Bitmap( bitmapData );
     return bitmap;
}
Advertisements

Written by Deepanjan Das

February 1, 2011 at 8:40 AM

One Response

Subscribe to comments with RSS.

  1. It’s helpful thanks.
    And I slightly changed to new Matrix( 1, 0, 0, 1 , -area.x , -area.y )

    locus

    September 30, 2015 at 5:58 PM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: