2、Flex中Image控件相关之(FileReference.save())保存图像到硬盘
Flex中Image控件相关之(FileReference.save())保存图像到硬盘
相关类:文件的保存FileReference.save(),组件的截屏BitmapData 类
上代码
?package comp.util{import flash.display.Bitmap;import flash.display.BitmapData;import flash.geom.Rectangle;import flash.utils.ByteArray;import mx.core.UIComponent;public class ImageProcessUtil{public function ImageProcessUtil(){}//将可视的UIComponent组件转换为ByteArraypublic static function BitmapDataToByteArray(target : UIComponent):ByteArray{var width :int = target.width;var height :int = target.height;var bitmapData:BitmapData = UIComponentToBitmapData(target);var byteArr:ByteArray = bitmapData.getPixels(new Rectangle(0,0,width,height));byteArr.writeShort(width);byteArr.writeShort(height);return byteArr;}//将可视的UIComponent组件转换为图片public static function UIComponentToBitmapData(target : UIComponent):BitmapData{var width :int = target.width;var height :int = target.height;var bitmapData:BitmapData =new BitmapData(width,height);bitmapData.draw(target);return bitmapData;}//将ByteArray转为bitMappublic static function ByteArrayToBitmap(byteArr:ByteArray):Bitmap{ byteArr.position = byteArr.length -2;var height:int = byteArr.readShort();byteArr.position = byteArr.length -4;var width:int = byteArr.readShort();var bitmapData:BitmapData = new BitmapData(width,height);byteArr.position = 0;for(var i:int = 0;i<height;i++){for(var j:int = 0;j<width;j++){bitmapData.setPixel(j,i,byteArr.readUnsignedInt());}}var bitMap:Bitmap = new Bitmap(bitmapData);return bitMap;}}}?