Adobe Education Store

Scaling an image - resizing a Bitmap

by admin on November 7, 2008 · 3 comments

in Flash CS3, Mix, Tutorials

When a bitmap (image) is reduced by using the methods scaleX and scaleY or width and height, the same bitmap seems to have changed but the size really does not.
This is because the BitmapData of the image has remained the same, then the weight and memory are same even if we had scaled down the image to 10%.

To resize and then actually gain weight and memory we must use the draw method of BitmapData class (of which we have already seen an example) and the Matrix class.

Very useful to create thumbnails.

The steps are as follows:

  1. Retrieve a temporary reference to the original Bitmapdata object.
  2. Draw a scaled version of the original BitmapData object into a new BitmapData object.
  3. Associate the original Bitmap object with the new, scaled Bimapdata object.

Here is the code:

1
2
3
4
5
6
7
8
9
var originalBitmapData:BitmapData=originalBitmap.bitmapData;
var scaleFactor:Number=0.5;
var newWidth:Number=originalBitmapData.width*scaleFactor;
var newHeight:Number=originalBitmapData.height*scaleFactor;
var scaledBitmapData:BitmapData=new BitmapData(newWidth,newHeight,true,0xFFFFFFFF);
var scaleMatrix:Matrix=new Matrix();
scaleMatrix.scale(scaleFactor,scaleFactor);
scaledBitmapData.draw(originalBitmapData,scaleMatrix);
originalBitmap.bitmapData=scaledBitmapData;
Share This Post

Related posts

{ 3 comments… read them below or add one }

1

Gonzalo 12.04.08 at 10:29 pm

Thanks a lot, man! it worked.

2

J 02.07.09 at 5:56 pm

thanks for this snippet, should be in the docs!

3

Sean McAuliffe 03.09.09 at 9:24 pm

This seems cool and I’ve tried a small example but could you give me an example of why I would do this? I thought I would see some difference in file size or something — when you say “actually gain weight and memory” could you be more specific?

Thanks in advance.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Older post: Getting the page url where my SWF is located

Newer post: Brief Introduction to Flash CS4