Topic: Smoother image resize in flash

I worked out a way to produce better looking images when resizing with the flash runtime. The images more closely resemble the end quality of the gears runtime and .ASP.NET GDI+ functions I already use on my site.

The blurfilter is used to blur the original before resizing to achieve less pixelated looking images. The block which sets filterAmount simply sets it to blur less depending on how much scaling is needed, the actual settings are up to the end user, but the idea is to apply less blur if images are less significantly downsampled.

Revised code is below. Note- to see changes to the flash runtime, you can't just retype this code in file.as, you also must recompile the project. This requires flashdevelop and the flex runtime to recompile the plupload.flash.swf ...

I am a *former* actionscript guru but I have done very little since cs3 and flex... just trying to wrap my head around all of this again and relearn the fundamentals of AS3, flex, etc. Just wondering if anyone has a better/simpler/more elegant approach to this, this seems to work well after minor testing, but seems a little heavy-handed to me. Major PITA to figure out a solution that worked, so hopefully this will help someone else in need.

Around line 39 (Imports section) of file.as:
    import flash.filters.BlurFilter;
    import flash.filters.BitmapFilterQuality;
    import flash.geom.Point;
    import flash.geom.Rectangle;

Starting around line 139 in file.as
...
if (scale < 1) {
               
//set rect and pt for filter area           
var rect:Rectangle = new Rectangle(1, 1, loadedBitmapData.width, loadedBitmapData.height);
var pt:Point = new Point(1, 1);

//set filteramount inversely proportional to resize amount
var filterAmount:Number
if (scale < .1) {
    filterAmount = 5
}else if (scale < .25) {
    filterAmount = 4
}else if (scale < .50) {
    filterAmount = 3
}else if (scale < .75) {
    filterAmount = 2
}else {
    filterAmount = 1
}                           
//create blurfilter with variable filterAmount of blur                       
var blurFilter:BlurFilter = new BlurFilter(filterAmount, filterAmount, BitmapFilterQuality.HIGH);
                           
//create working bitmap to apply filter to. Does not work if applying direct to loadedBitmap for some reason                           
var workingBitmapData:BitmapData = new BitmapData(loadedBitmapData.width, loadedBitmapData.height);
workingBitmapData.draw(loadedBitmapData, matrix, null, null, null, true);
workingBitmapData.applyFilter(workingBitmapData, rect, pt, blurFilter);
   
width = Math.round(loadedBitmapData.width * scale);
height = Math.round(loadedBitmapData.height * scale);
                   
// Setup scale matrix
matrix.scale(width / loadedBitmapData.width, height / loadedBitmapData.height);
                           
// Draw workingbitmap into scaled down bitmap
var outputBitmapData:BitmapData = new BitmapData(width, height);
outputBitmapData.draw(workingBitmapData, matrix, null, null, null, true);
                                                       
...

Re: Smoother image resize in flash

This is interesting stuff. Will try to put it in there. I'm no flash developers so the AS code might need a lot of improvement.