How to crop with AS3 (7)
May 5th, 2009, under Actionscript, Code snippets.
Quick post; How to crop the bitmap of your stage with AS3:
function crop( _x:Number, _y:Number, _width:Number, _height:Number, displayObject:DisplayObject = null):Bitmap
{
var cropArea:Rectangle = new Rectangle( 0, 0, _width, _height );
var croppedBitmap:Bitmap = new Bitmap( new BitmapData( _width, _height ), PixelSnapping.ALWAYS, true );
croppedBitmap.bitmapData.draw( (displayObject!=null) ? displayObject : stage, new Matrix(1, 0, 0, 1, -_x, -_y) , null, null, cropArea, true );
return croppedBitmap;
}
{
var cropArea:Rectangle = new Rectangle( 0, 0, _width, _height );
var croppedBitmap:Bitmap = new Bitmap( new BitmapData( _width, _height ), PixelSnapping.ALWAYS, true );
croppedBitmap.bitmapData.draw( (displayObject!=null) ? displayObject : stage, new Matrix(1, 0, 0, 1, -_x, -_y) , null, null, cropArea, true );
return croppedBitmap;
}
Usage:
var myCroppedImage:Bitmap = crop( 100, 100, 200, 200 );
this.addChild ( myCroppedImage );
this.addChild ( myCroppedImage );
To save the capture, use the ImageSaver class
Update: I’ve added a downloadable CropUtil class to googlecode.
Tagged with as3, snippet. | Tiny URL http://tinyurl.com/o8jmof






















nice work. Its really helpful.
it automatic take widh & height from Object. If you add option to add width & height custom & save according to that size that will be more helpful. (e.g resize).
great – thanks
Hello:
It is posible crop google Maps to a Bitmap in Flash. or Yahoo Maps. I have more problems with the api of google maps because when you put the map the display object have a mask but is more big.
thanks for all
@israel: Did you try it? I think it is possible, because I think the google map (in flash) is a displayobject too. Only thing is that is wont update, till you crop again. I guess cropping each frame is cpu intensive. this crop is not suppose to be mask.
Please also take a look at this: http://code.google.com/apis/maps/faq.html#bitmapdata
thank for this great tips, its very helpful.
Hello,
I have gone thru some of examples and i find them interesting.
I am trying to develop a crop tool having rectangle, circular, triangular kind of shapes. I did the
rectagular crop using copyPixel(). But I am not getting the other crop-shapes.
Can you help me out of this problem by giving suggestions or some hints.
I hope you will.
rgds,
Satish
If you used copyPixel(), then you have a bitmap object? imagine your bitmap is named myBitmap, then this would be the code:
var myCroppedImage:Bitmap = crop( 100, 100, 200, 200, myBitmap );
this.addChild ( myCroppedImage );