Stroep

Just a collection of random works – Mark Knol

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;
}
 

Usage:

var myCroppedImage:Bitmap = crop( 100, 100, 200, 200 );
this.addChild ( myCroppedImage );
 

To save the capture, use the ImageSaver class

Update: I’ve added a downloadable CropUtil class to googlecode.

Tagged with , . | Tiny URL http://tinyurl.com/o8jmof

 

11 Responses to “How to crop with AS3”

  1. 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).

  2. martin says:

    great – thanks

  3. israel says:

    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

  4. Mark says:

    @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

  5. thank for this great tips, its very helpful.

  6. satish says:

    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

  7. Mark Knol says:

    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 );

  8. ramel says:

    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?

  9. ramel says:

    Oh add I’m trying to copy a portions of the stage.

  10. ramel says:

    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.

  11. 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);

Trackbacks

    Leave a Reply