Smoothing loaded bitmaps in AS2

In Actionscript 2, there is a niggling annoyance with scaling bitmaps images (i.e. JPEG, PNG or GIF images). If such an image is in your Flash movie's library, you can tick the "Allow Smoothing" option; this means that when the image is scaled (up or down) the Flash player does its best to eliminate the jagged, "bitty" look of the resized image - the results are normally pretty good.

However, if you're loading in an image directly - for example, using MovieClipLoader - there is no way to turn on smoothing for that image.

The following snippet of code is a workaround for this problem. Call the function BitmapUtils.smoothLoadedImage(clip) on the clip that you loaded the image into, and it'll be smooth when scaled.

/** Image smoothing workaround
* By Ian Thomas at Awen, 2006
*/
import flash.display.BitmapData;
 
class com.awen.utils.BitmapUtils
{
    /** Workaround for a bug in AS2 whereby it's impossible to add smoothing to
    * a loaded bitmap (i.e. one that's not in the library).
    * @param target The loaded bitmap - calling this in onLoadInit would be appropriate.
    */
    public static function smoothLoadedBitmap(target:MovieClip):Void
    {
        // Adjust scaling to make sure we're getting as
        // high res a copy as possible
        var xs:Number=target._xscale;
        var ys:Number=target._yscale;
        target._xscale=100;
        target._yscale=100;
 
        // Copy the loaded bitmap into a new bitmap
        var bitmap:BitmapData=new BitmapData(target._width,target._height,true,0);
        bitmap.draw(target);
 
        // Paste it over the top of the loaded bitmap - as a child MC.
        var mc:MovieClip=target.createEmptyMovieClip(target._name+"bmap",target.getNextHighestDepth());
        mc.attachBitmap(bitmap,100,"never",true);  // The critical parameter is 'true' - this allows smoothing
 
        // The next two lines ensure the bitmap data gets disposed when the clip is unloaded.
        mc.bitmap=bitmap;
        mc.onUnload=function(){this.bitmap.dispose();}
 
        // Reset scaling
        target._xscale=xs;
        target._yscale=ys;
    }
}

Comments

I figured a way around my

I figured a way around my problem. I added a second listener to the MovieClipLoader and fired what I needed to do when that onLoadComplete listener was invoked... :)

Whoops! I just noticed that

Whoops! I just noticed that the function is different here. Here is the way mine is working:

var bitmap_1:BitmapData;
 
function loadBitmap1Smoothed(url1:String, target1:MovieClip) {
 
    // Create a movie clip which will contain our unsmoothed bitmap
    var bmc1:MovieClip = target1.createEmptyMovieClip("bmc1", target1.getNextHighestDepth());
 
    // Create a listener which will notify us when the bitmap loaded successfully
    var listener1:Object = new Object();
 
    // Track the target
    listener1.tmc1 = target1;
 
    // If the bitmap loaded successfully we redraw the movie into a BitmapData object and then attach 
    // that BitmapData to the target movie clip with the smoothing flag turned on.
    listener1.onLoadInit = function(mc1:MovieClip) {
		mc1._visible = false;
		bitmap_1.dispose();
		bitmap_1 = new BitmapData(mc1._width, mc1._height, true);
		this.tmc1.attachBitmap(bitmap_1, this.tmc1.getNextHighestDepth(), "auto", true);
		bitmap_1.draw(mc1);
    };
 
    // Do it, load the bitmap now
    var loader1:MovieClipLoader = new MovieClipLoader();
    loader1.addListener(listener1);
    loader1.loadClip(url1, bmc1);
}

I'm trying to wait until the bitmap is fully loaded to run the listener1 code, but I can't figure out why it won't work if I use onLoadComplete instead of onLoadInit...?

I'm using your function with

I'm using your function with a slight adjustment. I moved "var bitmap:BitmapData" out of the function and then added a bitmap.dispose() inside the function so that each time the function is run to attach an image it deletes the previous image.

My question is, why does the function not work when I change the listener.onLoadInit function to listener.onLoadComplete?

Thanks for posting this, it solved a problem I had for a long time with getting dynamically-loaded images to scale nicely...