Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 - Scale BitmapData

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?

like image 505
Tom Avatar asked May 23 '12 14:05

Tom


2 Answers

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

    }
}
}
like image 103
OXMO456 Avatar answered Oct 16 '22 02:10

OXMO456


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?

like image 25
Barış Uşaklı Avatar answered Oct 16 '22 00:10

Barış Uşaklı