07
Dec
07

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

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.


4 Responses to “Using the ImageSnapshot class to capture images as JPEGs or PNGs in Flex 3”


  1. 1 jon Dec 13th, 2007 at 12:57 am

    great site

    visit this website also: http://www.flexexamples.blogspot.com

    thanks
    jon

    http://www.flexexamples.blogspot.com

  2. 2 Tony Mar 17th, 2008 at 7:36 am

    Hi Peter,

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

    Thanks,
    Tony

  3. 3 peterd Mar 17th, 2008 at 1:49 pm

    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″.

  4. 4 Indigo Sanj Apr 16th, 2008 at 7:27 am

    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

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".