<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:net="flash.net.*"
    viewSourceURL="srcview/index.html"
    width="530" height="590"
    layout="vertical"     
    fontSize="12" 
    creationComplete="init()">
    
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.graphics.codec.JPEGEncoder;                        
            
            [Bindable]
            private var arr:Array=new Array();
            private var file:FileReference = new FileReference();
            
            private function init():void{
                //讀取本機端字型
                arr=Font.enumerateFonts(true);
                arr.sortOn("fontName", Array.CASEINSENSITIVE);
                var arrFont:Array=new Array;
                for(var i:int=0; i < arr.length; i++)
                {
                    arrFont.push(arr[i].fontName);
                }
                arr=arrFont;
            }
            
            //瀏覽本機圖檔
            private function openFileClick():void{
                //支援瀏覽的副檔名
                var imagesFilter:FileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
                this.fr.browse([imagesFilter]);
            }
        
            //選擇圖檔
            private function selectHandler(event:Event):void{
                this.fr.load();
            }
            
            //開啟後將圖檔載入到image中
            private function completeHandler(event:Event):void{
                image.source = this.fr.data;                
            }
            
            //儲存檔案
            private function savePic():void{
                if(fileNameTxt.text.length > 0){
                    var bitmapData:BitmapData = new BitmapData(pic.width, pic.height);
                     bitmapData.draw(pic,new Matrix());
                     var bitmap : Bitmap = new Bitmap(bitmapData);
                     bitmap.smoothing=true;
                    var jpg:JPEGEncoder = new JPEGEncoder(100);
                    var ba:ByteArray = jpg.encode(bitmapData);
                    file.save(ba,fileNameTxt.text + '.jpg');
                } else {
                    Alert.show("請輸入檔名","錯誤XD~");
                }
            }            
            
            private function fontFamily():void
            {
                T.setStyle("fontFamily",this.myfonts.selectedLabel);
            }
        ]]>
    </mx:Script>
    <net:FileReference id="fr" 
         select="selectHandler(event)"
         complete="completeHandler(event)"/>
    <mx:Button label="開啟本機圖檔" click="openFileClick();"/>
    
    <mx:Canvas id="pic" width="450" height="450" backgroundColor="0xffffff" 
        horizontalScrollPolicy="off" verticalScrollPolicy="off">
        <mx:Image id="image" source="@Embed('default.jpg')" width="440" height="440" 
             maintainAspectRatio="true" verticalAlign="middle" horizontalAlign="center"/>
        
        <mx:Label id="T"  y="43" fontSize="21"
            text="梅問題教學網【http://www.minwt.com】"
            horizontalCenter="0" width="100%" textAlign="center"/>    
    </mx:Canvas>
    
    <mx:VBox>
        <mx:HBox>
            <mx:Label text="字體"/>
            <mx:ComboBox id="myfonts" dataProvider="{arr}" change="fontFamily()"/>
        </mx:HBox>    
        <mx:HBox horizontalCenter="0">
            <mx:Label text="檔名"/>
            <mx:TextInput id="fileNameTxt" />
            <mx:Button click="savePic()" label="儲存"/>        
        </mx:HBox>
    </mx:VBox>

</mx:Application>