views:

332

answers:

2

So, I have bitmapA which is rectangular. I have a crop area I want to copy...

However, the bitmap is at an angle from the crop.

How do I copy a section from a bitmap that isn't a rectangle laid outon the x, y, axis??

Or copy custom shape????

Thanks

A: 

simplest way i can think of doing it is getting a temporary bitmap and use the draw method to duplicate the angled bitmap, then crop in from that. not the most memory efficient, but should work.

shortstick
+1  A: 

If you need to crop a rectangle with rotation, you can use draw method as suggested by shortstick to crop the red area as shown below:

alt text

var crop_rect:Rectangle = new Rectangle(0,0,64,57);//size of the segment to copy
var crop_point:Point = new Point(40,50);//relative position of the crop from the top/left corner of the image
var crop_angle:Number = Math.PI / 12;//angle of the crop relative to image in radians (clockwise)

//transformation [tx,ty] parameters representing shift after rotation
var dA:Number = Math.atan(crop_point.y / crop_point.x) - crop_angle;
var tX:Number = crop_point.length * Math.cos(dA);
var tY:Number = crop_point.length * Math.sin(dA);

var scaleMatrix:Matrix = new Matrix(Math.cos( -  crop_angle),Math.sin( -  crop_angle), -  Math.sin( -  crop_angle),Math.cos( -  crop_angle), -  tX, -  tY);
var colorTransform:ColorTransform = new ColorTransform();//no colour transformation needed

//copy selected segment after rotation and shift to match the size of the crop
var result_bitmap = new BitmapData(crop_rect.width,crop_rect.height);
result_bitmap.draw(source_img, scaleMatrix , colorTransform, null, crop_rect, true);
var result_img:Bitmap = new Bitmap(result_bitmap);

The result is below: alt text Hope that is what you were looking for, otherwise please give more details and, even better, a visual sample.

Alex