The following example shows how you can use the ImageSnapshot class to capture images as JPEGs or PNGs by setting the encoder parameter in the ImageSnapshot class’s static captureImage() method in Flex 3.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/12/07/using-the-imagesnapshot-class-to-capture-images-as-jpegs-or-pngs-in-flex-3/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.graphics.ImageSnapshot;
            import mx.graphics.codec.*;

            private const jpgEnc:JPEGEncoder = new JPEGEncoder();
            private const pngEnc:PNGEncoder = new PNGEncoder();

            private function captureImg(imgEnc:IImageEncoder):void {
                var ohSnap:ImageSnapshot;
                ohSnap = ImageSnapshot.captureImage(img, 0, imgEnc);
                textArea.text = ImageSnapshot.encodeImageAsBase64(ohSnap);
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:ToggleButtonBar id="toggleButtonBar"
                itemClick="captureImg(event.item.data);">
            <mx:dataProvider>
                <mx:Array>
                    <mx:Object label="Capture as JPEG" data="{jpgEnc}" />
                    <mx:Object label="Capture as PNG" data="{pngEnc}" />
                </mx:Array>
            </mx:dataProvider>
        </mx:ToggleButtonBar>
    </mx:ApplicationControlBar>

    <mx:Image id="img"
            source="@Embed('images/flex_logo.jpg')" />

    <mx:TextArea id="textArea"
            editable="false"
            width="320"
            height="160" />

</mx:Application>

View source is enabled in the following example.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

7 Responses to Using the ImageSnapshot class to capture images as JPEGs or PNGs in Flex 3

  1. Tony says:

    Hi Peter,

    Thats a great example , but can we create the vice-verca of that means creating image from this binary data.

    Thanks,
    Tony

  2. peterd says:

    Tony,

    I’ll try and come up with an example tonight, but you should be able to use a combination of the Base64Decoder class’s decode() and toByteArray() methods to convert a base64 string into a byte array, and then you can display the byte array in a Flex 3 Image control using the load() method.

    Peter

    UPDATE: For more information, see “Displaying an image saved as a Base64 encoded string in Flex 3″.

  3. Indigo Sanj says:

    hi! Is this the quickest way to create a png from a Flex component? I am trying to create a button such as “view PNG” and have my flex chart opened in a web-browser window as a png.

    any ideas

  4. Anonymous says:

    I’m trying to do this in flash using flash develop but publishing through adobe ide. I should be able to use the base64 methods since I imported the swc which has them, it compiles telling me that it can find the signatures but I get the following runtime error, im using a path to the library like C:\work\myproject\libs\as3core and in that location there is the swc as3corelib.swc

    Class mx.utils::Base64Encoder could not be found.
    thanks for any help

  5. Tahir Alvi says:

    Hi peter.

    I use this same approach in Flex 3 with sdk 3.2, but i got error.

    Are any limitation in SDK 3.2 for image encoder.

    Thanks.

  6. Gerry says:

    Does anyone know of an issue where you use this to take a snapshot then all event listeners applied to the MovieClip you just took a snapshot of now no longer has the event listeners active?
    I am using ImageSnapshot like this…
    snap_image = ImageSnapshot.captureImage(txtContainer, 300, new PNGEncoder());
    ResponseString = ImageSnapshot.encodeImageAsBase64(snap_image);

    txtContainer is a movieClip that contains some dynamically created objects and it has eventListeners for roll over, out and click. Those no longer work after the snapshot is taken even when I try to reapply the listeners.
    Any tips?