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'); }
Thanks a lot that saved a lot of time 😀