经过两天的艰苦琢磨,终于搞出来了。
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" initialize="application1_initializeHandler(event)"> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <fx:Script> <![CDATA[ import flash.desktop.Clipboard; import flash.desktop.ClipboardFormats; import flash.display.Sprite; import flash.net.URLRequest; import flash.system.System; import flash.system.fscommand; import mx.controls.Alert; import mx.controls.Image; import mx.events.FlexEvent; import mx.graphics.ImageSnapshot; import mx.graphics.codec.JPEGEncoder; import mx.utils.Base64Encoder; [Bindable] private var imageData:ByteArray; //调用js来把图片复制到剪贴板 private static var function_setToJs:String="function(value){" + " var img=document.createElement('img');" + " var imgsrc='data:image/jpg;base64,'+value;" + " img.src=imgsrc;" + " img.contentEditable = 'true';" + " document.body.appendChild(img);" + " img.style.display='none';" + " var controlRange;" + " if (document.body.createControlRange) {" + " controlRange = document.body.createControlRange();" + " controlRange.addElement(img);" + " controlRange.execCommand('copy');}" + " img.contentEditable = 'false';" + "}"; private function cutMap(e:MouseEvent):void { this.cutmap.visible=false; var img:BitmapData = ImageSnapshot.captureBitmapData(this); var jpg:JPEGEncoder=new JPEGEncoder(50); imageData=jpg.encode(img); // snap=ImageSnapshot.encodeImageAsBase64(ImageSnapshot.captureImage(this,new JPEGEncoder)) this.savemap.visible=true; this.copymap.visible=true; } private function seveMap(e:MouseEvent):void{ var fr:FileReference=new FileReference(); fr.save(imageData,"map.jpg"); this.cutmap.visible=true; this.savemap.visible=false; this.copymap.visible=false; } protected function application1_initializeHandler(event:FlexEvent):void { // Todo Auto-generated method stub this.cutmap.visible=true; this.savemap.visible=false; this.copymap.visible=false; } protected function copyMap(event:MouseEvent):void{ var base64:Base64Encoder = new Base64Encoder(); base64.encodeBytes(imageData); var base:String=base64.toString(); ExternalInterface.call(function_setToJs,base); } ]]> </fx:Script> <mx:VBox width="100%" height="100%"> <mx:HBox width="100%"> <mx:Button id="cutmap" x="80" width="50" label="截图" buttonMode="true" click="cutMap(event)" fontSize="12"/> <mx:Button id="savemap" width="50" label="另存为" buttonMode="true" click="seveMap(event)" fontSize="12"/> <mx:Button id="copymap" width="50" label="复制" buttonMode="true" fontSize="12" click="copyMap(event)"/> </mx:HBox> </mx:VBox> </s:Application>