In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

BitmapDataのPNG, Jpeg, JpegXRへの変換

Flash Player 11.3 から対応した BitmapData.encode()を利用したPNG, JPEG, JPEGXR への変換サンプルを追加
http://morishige.jp/blog/archives/322
/**
 * Copyright MORISHIGE ( http://wonderfl.net/user/MORISHIGE )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/k4MV
 */

// forked from kihon's flash on 2010-4-24
package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.FileReference;
    import flash.utils.ByteArray;
    import mx.graphics.codec.PNGEncoder;
    import mx.graphics.codec.JPEGEncoder;
    import com.bit101.components.PushButton;
    
    public class Main extends Sprite
    {    
        private var canvas:BitmapData;

        public function Main()
        {
            canvas = new BitmapData(100, 100, false, 0xED1A3D);
            var bitmap:Bitmap = new Bitmap(canvas);
            addChild(bitmap);
            bitmap.x = 182;
            bitmap.y = 182;
            
            new PushButton(this, 182, 282, "旧方式 save(PNG)", onMouseClick);
            new PushButton(this, 182, 302, "旧方式 save(JPEG)", onMouseClick2);
            new PushButton(this, 182, 322, "FP11.3以降 save(PNG)", onMouseClick3);
            new PushButton(this, 182, 342, "FP11.3以降 save(JPEG)", onMouseClick4);
            new PushButton(this, 182, 362, "FP11.3以降 save(JPEGXR)", onMouseClick5);
        }

        private function onMouseClick(event:Event):void 
        {
            var ba:ByteArray = new PNGEncoder().encode(canvas);
            new FileReference().save(ba, "t.png");
        }
        
        private function onMouseClick2(event:Event):void 
        {
            var ba:ByteArray = new JPEGEncoder().encode(canvas);
            new FileReference().save(ba, "t.jpeg");
        }
        
        private function onMouseClick3(event:Event):void {
            var ba:ByteArray = new ByteArray();
            canvas.encode(canvas.rect, new flash.display.PNGEncoderOptions(), ba);
            new FileReference().save(ba, "t2.png");
        }
        private function onMouseClick4(event:Event):void {
            var ba:ByteArray = new ByteArray();
            canvas.encode(canvas.rect, new flash.display.JPEGEncoderOptions(), ba);
            new FileReference().save(ba, "t2.jpeg");
        }
        private function onMouseClick5(event:Event):void {
            var ba:ByteArray = new ByteArray();
            canvas.encode(canvas.rect, new flash.display.JPEGXREncoderOptions(), ba);
            new FileReference().save(ba, "t2.jxr");
        }
    }
}