import com.adobe.images.JPGEncoder;
import com.adobe.images.PNGEncoder;
import flash.filesystem.*;
jpg_btn.addEventListener(MouseEvent.CLICK, saveImg);
png_btn.addEventListener(MouseEvent.CLICK, saveImg);
function saveImg(e:MouseEvent):void {
var tname = e.target.name;
var file:File = File.applicationResourceDirectory;
var bpd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
var ba:ByteArray;
bpd.draw(root);
if(tname == "jpg_btn"){
var jpg:JPGEncoder = new JPGEncoder(80);
ba = jpg.encode(bpd);
file = file.resolve("test.jpg");
}else if(tname == "png_btn"){
ba = PNGEncoder.encode(bpd);
file = file.resolve("test.png");
}
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(ba);
fileStream.close();
}
原始檔: saveImgFile.fla (as3corelib 已經包含在原始檔內)
沒有留言:
張貼留言