Resize a movieclip snapshot in ActionScript 3.0

Let’s say you want to create a snapshot of a movieclip inside your Flash/Flex application. The constraint here is you need to resize the snapshot to the values (width and height) entered by user. Here is how you could do this :

var bitmapData:BitmapData = new BitmapData(clip.width, clip.height);                //clip is a movieclip whose snapshot is to be taken
bitmapData.draw(clip);

var bitmap:Bitmap = new Bitmap(bitmapData.clone());
bitmap.width = int(width);                 //rounding width and height to integer values
bitmap.height = int(height);              //This is important to resize the child (bitmap) here as you can not resize the sprite (sp) itself as sprite is going to be drawn inside bitmap data.
var sp:Sprite = new Sprite();
sp.addChild(bitmap);

var bmd:BitmapData = new BitmapData(sp.width, sp.height);                //As the child of sprite is already resized it will think that actual width and height of sprite are unscaled and hence it can map pixel to pixel on full width and height.
bmd.draw(sp);          //now draw sprite over bitmap data.

var encoder:JPGEncoder = new JPGEncoder(100);
var byteArray:ByteArray = encoder.encode(bmd);           //encode bitmap data using adobe’s corelib’s JPGEncoder to get byte array.

That’s it. Again keep in mind that the clip which you are going to draw over bitmap data should not be resized because that will not affect the drawing of bitmap data and you could see some unusual results.

There may be other ways to achieve this 🙂 but I found (may be others found before me) this method convenient for me.

Thanks,

Naresh Khokhaneshiya

Leave a Reply

Your email address will not be published. Required fields are marked *