views:

5033

answers:

5

Say I have a BitmapData of 600x600 and I want to scale it down to 100x100.

+1  A: 

Without writing the code myself. The way i would approach this would be to create a new BitmapData object of the desired size and then use the bitmap.draw method to copy the large one to the small one. The bitmap.draw method also accepts a matrix argument that you can use to scale when you copy.

James Hay
+6  A: 

This works:

var scale:Number = 0.32;
var matrix:Matrix = new Matrix();
matrix.scale(scale, scale);

var smallBMD:BitmapData = new BitmapData(bigBMD.width * scale, bigBMD.height * scale, true, 0x000000);
smallBMD.draw(bigBMD, matrix, null, null, null, true);

var bitmap:Bitmap = new Bitmap(smallBMD, PixelSnapping.NEVER, true);
Iain
+4  A: 
public function drawScaled(obj:DisplayObject, thumbWidth:Number, thumbHeight:Number):Bitmap {
    var m:Matrix = new Matrix();
    m.scale(WIDTH / obj.width, HEIGHT / obj.height);
    var bmp:BitmapData = new BitmapData(thumbWidth, thumbHeight, false);
    bmp.draw(obj, m);
    return new Bitmap(bmp);
}

from: http://www.nightdrops.com/2009/02/quick-reference-drawing-a-scaled-object-in-actionscript/

kajyr
Not actually what I wanted, because I was starting with a bitmapdata rather than a display object. thanks though!
Iain
easily fixed by subtituting DisplayObject with BitmapData ;-)
kajyr
this one worked for me :-)
dta
A: 

The problem with using matrix scaling is that it doesn't do any antialiasing or smoothing - this is probably OK if you're sure you will only ever been downscaling, but a more general method would use the Image class to do the resizing. In AS3 this would never be added to the display list, so would just be used "offscreen". Something like this (with your bitmap data as "sourceBitmapData"):

var image:Image = new Image();
image.load(new Bitmap(sourceBitmapData, PixelSnapping.NEVER, true));

var scale:uint = 100/600; // this is from your example of 600x600 => 100x100
var scaledWidth:uint = sourceBitmapData.width * scale;
var scaledHeight:uint = sourceBitmapData.height * scale;

image.content.width = scaledWidth;
image.content.height = scaledHeight;

var scaledBitmapData:BitmapData = new BitmapData(scaledWidth, scaledHeight);
scaledBitmapData.draw(image.content); 

image = null;

You can then use "scaledBitmapData" in place of "sourceBitmapData" to do whatever with.

A: 

great answer, thanks

Rodislav Moldovan