How to set JPG type of image screenshot saved from

2019-07-19 06:27发布

问题:

I have a problem when i wanna save screenshot entire flash stage using ActionScript 3. when i click button that use function SimpanGbr it's works for saving image with name "NamaGambar" but without type of JPG image. How to save image with it's type. I use these codes:

stop();
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import com.adobe.images.JPGEncoder;
stage.displayState = StageDisplayState.FULL_SCREEN;

exit.addEventListener(MouseEvent.CLICK,keluar);

function keluar(event:MouseEvent):void
{
    fscommand("quit");
}

function SimpanGbr(e:MouseEvent):void {
    var qImageData:BitmapData = new BitmapData(800, 600);
    qImageData.draw(stage);
    var qEncoder:JPGEncoder = new JPGEncoder(100);
    var qBytes:ByteArray = qEncoder.encode(qImageData);
    var qFile:FileReference = new FileReference();
    var nama:String="NamaGambar";
    qFile.save(qBytes, nama+".jpg");
}

回答1:

You can do so using this library.

After downloading copy the source files (the com folder) on to your Class path or your application root directory. Apart from the JPEGEncoder, this library is packaged with a lot of other classes related to Hasing, Text, Date etc. This is how we save an image from a flash movie to a local machine.

1) Create a BitmapData object out of the MovieClip or the Loader.

2) Encode the BitmapData using the JPEGEncoder and form a ByteArray.

3) Use FileReference.save() to download the image on to the User machine.

See an example: Create a blank Flash movie and name it as “ImageSave.fla”. In the same folder create “ImageSave.as” file and copy the below code in to it. You need the “com” folder copied from the as3corelib to this folder. I have a button on the stage named “save_mc” which triggers the saveImage function.

package {
    import flash.display.MovieClip;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.utils.ByteArray;
    import flash.net.FileReference;
    import flash.net.FileFilter;
    import flash.events.*;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import com.adobe.images.JPGEncoder;
    public class ImageSave extends MovieClip {
        private var imgFile:URLRequest;
        private var img:MovieClip;
        private var imgLoader:Loader;
        private var file:FileReference;
        public function ImageSave() {
            imgFile = new URLRequest("coffee.jpg"); //change it to your image path
            imgLoader = new Loader();
            imgLoader.load(imgFile);
            imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
            save_mc.addEventListener(MouseEvent.CLICK, saveImage);
        }
        private function onLoaded(evt:Event):void {
            imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaded);
            imgLoader.cacheAsBitmap = true;
            addChild(imgLoader);
        }
        function saveImage(e:MouseEvent):void {
            var myBitmapData:BitmapData = new BitmapData(imgLoader.width, imgLoader.height);
            myBitmapData.draw(imgLoader);
            var jpgEncoder:JPGEncoder = new JPGEncoder(80);
            var imgByteData:ByteArray = jpgEncoder.encode(myBitmapData);
            file = new FileReference();
            file.browse(new Array(new FileFilter("Images (*.jpg, *.jpeg)", "*.jpg;*.jpeg")));
            file.save(imgByteData, "test.jpg");
        }
    }
}

For your convenience you can download a working copy of this sample here.