Actionscript 3: Export Screenshot as PNG

Taking screenshots of your stage in is quite easy. For my example you just need kaourantin’s PNGEnc Class.

Download the PNGEnc class here

The guys who did the as3corelib have a PNG and JPG encoder class as well, you can download as3corelib here if you want to use their classes instead.

Here my example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private function takeScreenshot():void 
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.utils.ByteArray;
import flash.net.FileReference;
import net.kaourantin.PNGEnc;
 
 
var bmd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0);
var bm:Bitmap = new Bitmap(bmd, "auto",true);
 
bmd.draw(this); // i wrote my example code in my document class, so "this" refers to my document class and the whole stage is drawn into the bitmapdata
 
var bild:ByteArray = PNGEnc.encode(bmd);
 
var file:FileReference = new FileReference();
 
file.save(bild, "screenshot.png");
 
trace("Taken Screenshot");
 
}