As we saw in a previous entry, “Taking screenshots in Flex 3 using the ImageSnapshot.captureImage() method”, it is possible to take a screenshot of an item on the display list using the static ImageCapture.captureImage() method. Once the image is captured, we accessed the image’s pixel data using the ImageCapture object’s data property, which was displayed using the SWFLoader control.

The ability to load a ByteArray object directly into a SWFLoader was added in Flex 3 build 187814, you may need to update your SDK build by downloading a recent nightly build from http://labs.adobe.com/technologies/flex/sdk/flex3sdk.html.

The following example shows how you can take a snapshot of an item on the display list using the static ImageSnapshot.captureBitmapData() method, which returns a BitmapData object, as seen in the following snippet:

var imageBitmapData:BitmapData = ImageSnapshot.captureBitmapData(source);
swfLoader.source = new Bitmap(imageBitmapData);

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/11/17/taking-screenshots-in-flex-3-using-the-imagesnapshotcapturebitmapdata-method/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

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

            private function takeSnapshot(source:IBitmapDrawable):void {
                var imageBitmapData:BitmapData = ImageSnapshot.captureBitmapData(source);
                swfLoader.source = new Bitmap(imageBitmapData);
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object col1="Row 1, Col 1" col2="Row 1, Col 2" />
        <mx:Object col1="Row 2, Col 1" col2="Row 2, Col 2" />
        <mx:Object col1="Row 3, Col 1" col2="Row 3, Col 2" />
        <mx:Object col1="Row 4, Col 1" col2="Row 4, Col 2" />
        <mx:Object col1="Row 5, Col 1" col2="Row 5, Col 2" />
        <mx:Object col1="Row 6, Col 1" col2="Row 6, Col 2" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Take snapshot of DataGrid"
                click="takeSnapshot(dataGrid);" />
    </mx:ApplicationControlBar>

    <mx:HBox>
        <mx:DataGrid id="dataGrid" dataProvider="{arr}" />
        <mx:SWFLoader id="swfLoader">
            <mx:filters>
                <mx:DropShadowFilter />
            </mx:filters>
        </mx:SWFLoader>
    </mx:HBox>

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

11 Responses to Taking screenshots in Flex 3 using the ImageSnapshot.captureBitmapData() method

  1. Dhaya says:

    I was kind of excited when i read the title on the RSS, thinking it would be now possible to make screenshots of the desktop. Though, that’s still very interesting !

  2. tom says:

    Hi there, like your blog.
    Could you please make a tutorial how to fetch data from a datagrid?
    I’d like to use data binding but have no idea how to this with a grid…
    Or if you have a list of items which have different prices in a datagrid.
    How do I calculate the sum of these prices?

    Regards, Tom

  3. tom says:

    Hello again, would you please be so kind and answer my question?
    Thanx a lot!

  4. peterd says:

    tom,

    To access data in a Flex DataGrid control, you can use the dataProvider property.
    Also, if you wanted to calculate the sum of the items in a data grid, you could loop over each item in the data provider and add it to another variable and then display the final sum in a Label control or however you wanted. Flex 3 has an “AdvancedDataGrid” which may automatically support summary rows (I haven’t used the AdvancedDataGrid control yet, so I’m not sure what it supports exactly, or which APIs you’d use).

    Also, there is an example of binding a data provider to a DataGrid in the Flex 3 documentation at http://livedocs.adobe.com/labs/flex3/langref/mx/controls/DataGrid.html.

    Hope that helps,
    Peter

  5. tom says:

    Thanx for your time! Will try that!

  6. tom says:

    I don’t get it, I give up…

  7. Thx, you’re one Great Man!

  8. I need to save a copy of the Bitmap Image as a file download, any ideas on how this can be achieved in the flash player without the need for a processing script.

    Great site by the way.

    A

  9. break says:

    plz help

    how to capture desktop image

  10. hari says:

    this is exactly what i need.

    kudos for you and many thanks!

  11. Andrew says:

    Nice and simple – great.

    Now, how would you save this snapshot as a JPEG or PNG?

    Andrew.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree