Even though there are many posts on this particular topic still there are many ???, then i thought may be i can write about it, so lets get started.
Lets do things in steps.
- Load Image in to Flex App
- Convert it to Bitmap and BitmapData objects.
- Crop Image using BitmapData object
1.Load Image in to Flex App
private var imageLoader:Loader = new Loader();
private function fnInit():void
{
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImageComplete);
imageLoader.load(new URLRequest(“assets/flower.png”));
}
2.Convert it to Bitmap and BitmapData objects
private var bitmapImage:Bitmap;
private var bitmapDataImage:BitmapData;
//Event.COMPLETE eventHandler for Loader
private function loadImageComplete(e:Event):void
{
bitmapImage = Bitmap(e.target.loader.content);
bitmapDataImage = Bitmap(bitmapImage).bitmapData.clone();
}
3.Crop Image using BitmapData object
[showmyads]
//Specifying the dimensions for Image cropping
private var WIDTH:Number=200;
private var HEIGHT:Number=150;
private var X:Number=50;
private var Y:Number=50;
private function getCroppedImage():BitmapData
{
var croppedBitmapData:BitmapData = new BitmapData(WIDTH, HEIGHT, false);
croppedBitmapData.copyPixels(bitmapDataImage, getImageRect(), new Point(0, 0));
return croppedBitmapData;
}
private function getImageRect():Rectangle
{
var requestedRect:Rectangle;
requestedRect = new Rectangle(X, Y, WIDTH, HEIGHT);
return requestedRect;
}
here the function getCroppedImage() returns the cropped Image BitmapData.And to see the cropped Image assign it to source of Image like below.
var croppedBitmap:Bitmap = new Bitmap(getCroppedImage());
image.source=croppedBitmap;//image is ‘id’ of Image
To better understand the Post i recommend reading Bitmap and BitmapData classes.
Enjoy the Post.
[…] This post was mentioned on Twitter by karannnnnnnnnnn3, Eric Snowden. Eric Snowden said: Image Cropping in Flex http://goo.gl/fb/gkUSp […]
Is there a way to make selection, like magmatic lasso tool around an image and then crop it and save as a .png file in flex.
Yes there are posts of your request, just google it you may find few, my post was to give basic info about cropping.thanks.
good code,
waiting for ur more post on flex 4.5
🙂
Thanks, actually haven’t got time lately.