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:
- Retrieve a temporary reference to the original Bitmapdata object.
- Draw a scaled version of the original BitmapData object into a new BitmapData object.
- 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; |









{ 4 comments… read them below or add one }
Thanks a lot, man! it worked.
thanks for this snippet, should be in the docs!
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.
Thanks a lot!
@Sean: For us, it came in handy as Flash’s Bitmap-class tends to draw incorrect BitmapData if their scaling is REALLY small (like 0,02).
{ 1 trackback }