Scaling a 600 x 300 bitmap to fit in a rectangle 300 x 300 is easy. But say that instead of getting a 300x150 bitmap, you wanted it 300x300 with the 300x150 bitmapdata centered vertically within the rectangle? Any suggestions?
Here's where I'm at so far...
public static function createThumb(bd:BitmapData, maxW:Number, maxH:Number, doCenter:Boolean = true):BitmapData{
// make the bitmap we'll use to get the dimensions
var bmp:Bitmap = new Bitmap(bd);
// store the new dimensions
var nW:int = Math.round(bmp.width);
var nH:int = Math.round(bmp.height);
// get the proportions for resizing
var ratio:Number = nW/nH;
// set the width
if (nW > maxW) {
nW = maxW;
nH = Math.round(nW / ratio);
}
// set the height
if (nH > maxH) {
nH = maxH;
nW = Math.round(nH * ratio);
}
// create the matrix we'll use to set scale and position
var matrix:Matrix = new Matrix();
// center the bitmapdata within the given width and height
if (doCenter){
var dx:Number = (maxW - nW)/2;
var dy:Number = (maxH - nH)/2;
matrix.translate(dx, dy);
}
// scale the thing
matrix.scale(nW / bmp.width, nH / bmp.height);
// define the bitmapdata
var bmpd:BitmapData = new BitmapData(maxW, maxH, true, 0xFFFFFFFF); // transparent (just in case)
bmpd.draw(bmp,matrix);
return bmpd;
}
Looks like setting matrix.scale prior to matrix.translate took care of it.
do the matrix scale before you do the translate...
..oh... nm 
Sorry, you must be a member to post to a conversation. Either
log in or
sign up
to get involved.