I'd like to scale a BitmapData to different sizes such as 200, 400, 600 and 800.
What is a good way to do that?
You can't directly scale a BitmapData
but you can make a scaled clone of it.
Here is a quick example for scaling a BitmapData
:
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Matrix;
import mx.core.BitmapAsset;
public class Test extends Sprite {
[Embed(source="test.jpg")]
private var Image:Class;
public function Test() {
var originalBitmapData:BitmapData = BitmapAsset(new Image()).bitmapData;
function scaleBitmapData(bitmapData:BitmapData, scale:Number):BitmapData {
scale = Math.abs(scale);
var width:int = (bitmapData.width * scale) || 1;
var height:int = (bitmapData.height * scale) || 1;
var transparent:Boolean = bitmapData.transparent;
var result:BitmapData = new BitmapData(width, height, transparent);
var matrix:Matrix = new Matrix();
matrix.scale(scale, scale);
result.draw(bitmapData, matrix);
return result;
}
var bitmapA:Bitmap = new Bitmap(originalBitmapData);
addChild(bitmapA);
var bitmapB:Bitmap = new Bitmap(scaleBitmapData(originalBitmapData, 0.5));
addChild(bitmapB);
}
}
}
Setting the width and height of a bitmap object scales that image, so create a bitmap with your bitmapData then scale it using width/height, or use scaleX/Y if you want to use scaling values.
var bmp:Bitmap = new Bitmap(bmpData);
bmp.width = 400; // or 600,800 etc.
bmp.height = 400;
If you don't want to use a bitmap object and directy scale the BitmapData see this What is the best way to resize a BitmapData object?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With