How to crop with AS3 (11)
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 );
I’m trying to use this class but I am getting a “1180 call to possibly undefined method crop” I am just doing a test to play with it so Im using the code example above.
import nl.stroep.utils.*
var myCroppedImage:Bitmap = crop( 100, 100, 200, 200 );
this.addChild ( myCroppedImage );
what am I screwing up?
Oh add I’m trying to copy a portions of the stage.
Ok got it too a look at the as file. the displayObject is the first method in the function not the last. This is a great little script very cool.
First, let me say that you are a god!
I was struggling with this, and would not have figured it out on my own.
Second, let me offer up something useful.
I needed this because of problems with masking, and I needed my cropped image to maintain its transparency. Substituting this small change into your code will make it preserve any existing transparency:
var croppedBitmap:Bitmap = new Bitmap(new BitmapData(_width, _height, true, 0×00000000), PixelSnapping.ALWAYS, true);