Setting a default encoder for the ImageSnapshot class in Flex 3

The following example shows how you can set the ImageSnapshot class’s static defaultEncoder property in Flex 3 to set a default image encoder for captured images.

Full code after the jump.


<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<mx:Application xmlns:mx=""


            private function init():void {
                ImageSnapshot.defaultEncoder = PNGEncoder;

            private function captureImg():void {
                var ohSnap:ImageSnapshot = ImageSnapshot.captureImage(img);
                textArea.text = ImageSnapshot.encodeImageAsBase64(ohSnap);
                lbl.text = ohSnap.contentType;

    <mx:ApplicationControlBar dock="true">
        <mx:Button id="button"
                label="Capture Image"
                click="captureImg();" />

        <mx:FormItem label="source:">
            <mx:Image id="img"
                    source="@Embed('images/flex_logo.jpg')" />
        <mx:FormItem label="Base64:">
            <mx:TextArea id="textArea"
                    height="160" />
        <mx:FormItem label="contentType:">
            <mx:Label id="lbl" />


View source is enabled in the following example.

6 thoughts on “Setting a default encoder for the ImageSnapshot class in Flex 3

  1. OK, Peter. Now what? What can I do with this string? Must I pass it to my server to save it as a file? Can I copy and paste the string into a empty text document and make it a PNG?

    I love your site, btw. I stopped tagging pages and just use it as a reference whenever I run into problems or when I have trouble getting a feature started.

  2. Hi Peter, thanks for the great article, altough I have the same question as Leif.
    What do you do with the String?

    Thanks for the great site! It’s one of the most useful resources on the web.

  3. Kim,

    I haven’t tried it, but I imagine you’d have to send the base64 encoded string to a server side script (ColdFusion, PHP, ASP, etc) which saves it to the server disk. Then you could have the user download the PNG file from the server using the FileReference class’s download() method.


Comments are closed.