AS3 - Scale BitmapData
Asked Answered
K

2

5

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?

Kala answered 23/5, 2012 at 14:40 Comment(0)
B
12

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);

    }
}
}
Barkeeper answered 24/5, 2012 at 10:10 Comment(0)
P
2

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?

Prunella answered 23/5, 2012 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.