Veltar
Veltar

Reputation: 741

Scaling and cropping an image with fixed dimensions

I want to display an image, and it should be transformed like this:

If for example, my original image is 200x300 pixels in size, I want it to have a width of 150, and then scale the height accordingly. Then I want to crop the image, so that the result has a dimension of 150x150 pixels.

I've tried several ways, but haven't figured out how to do it.

Upvotes: 2

Views: 3635

Answers (1)

weltraumpirat
weltraumpirat

Reputation: 22604

You can calculate the scale factor by dividing new width by old width:

var scale : Number = 150 / myImage.width;
myImage.scaleX = myImage.scaleY = scale;

To crop, either use a mask on the scaled clip, or draw to a new Bitmap:

var myBitmapData : BitmapData = new BitmapData ( 150, 150 );
// use concatenated matrix of the image to scale the new bitmap data
var matrix : Matrix = myImage.transform.concatenatedMatrix; 
myBitmapData.draw ( myImage, matrix );

var myBitmap : Bitmap = new Bitmap ( myBitmapData );
addChild ( myBitmap );

Depending on how many images there are, and what you are going to do with them later, you should always test both possibilities for performance.

Upvotes: 3

Related Questions