create a thumbnail of an image in actionscript

34 sec read

Assume test1.jpg is the original file and test1_thumb.jpg is the thumbnail file to be created. The width or height of the thumbnail is of maximal size 100.

private function loadBMP():void
{
	loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
	loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);

    var request:URLRequest = new URLRequest(File.desktopDirectory.resolvePath("test1.jpg").url);
    loader.load(request);
}
private function onComplete(e:Event):void
{
	var thumbSize:int = 100;
	var ratio:Number = 1;
	if(loader.width >= loader.height)
	{
		ratio = thumbSize/loader.width;
	}
	else
	{
		ratio = thumbSize/loader.height;
	}
		
	this.bd = new BitmapData( loader.width*ratio, loader.height*ratio, false);
	var matrix:Matrix = new Matrix();
	matrix.scale(ratio, ratio);
	this.bd.draw( loader.content, matrix );
	
	var jpg:JPEGEncoder = new JPEGEncoder();
	var ba:ByteArray = jpg.encode(this.bd);
	var f:File = File.desktopDirectory.resolvePath("test1_thumb.jpg");
	var fs:FileStream = new FileStream();
	fs.open(f, FileMode.WRITE);
	fs.writeBytes(ba);
	fs.close();
}
private function onIOError(e:IOErrorEvent):void
{
	trace('error happened');
}



文献鸟双 11 特惠


写作助手,把中式英语变成专业英文


Want to receive new post notification? 有新文章通知我

How much money did I make from an app?

Undoubtedly some people are very successful in making money by developing a smartphone app. Back in 2012 I developed an app called “Handbook of Brain” which is a collected resources of brain anatomy, function and diseases. I put the app i
Xu Cui
27 sec read

Handy programs to visualize NIRS data (2): plotTopoMap

Often you need to view the spatial pattern of activation as in the example below. plotTopoMap allows you to do that. It probably only works for Hitachi devices where the spatial relationship between channels are known. In the above example, the activ
Xu Cui
37 sec read

Flash 3D video demo

Racer Ostrova Zombie
Xu Cui
0 sec read

One Reply to “create a thumbnail of an image in actionscript”

Leave a Reply

Your email address will not be published. Required fields are marked *