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

by Peter deHaan on November 17, 2007

in Bitmap/BitmapData, BitmapData, ImageSnapshot

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.

{ 11 comments… read them below or add one }

1 Dhaya November 18, 2007 at 2:23 am

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 !

Reply

2 tom November 18, 2007 at 1:01 pm

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

Reply

3 tom November 20, 2007 at 6:34 am

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

Reply

4 peterd November 20, 2007 at 6:55 am

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

Reply

5 tom November 20, 2007 at 7:41 am

Thanx for your time! Will try that!

Reply

6 tom November 20, 2007 at 10:05 am

I don’t get it, I give up…

Reply

7 David Patrick June 3, 2008 at 4:49 pm

Thx, you’re one Great Man!

Reply

8 Andrew Hopkins July 11, 2008 at 6:38 am

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

Reply

9 break January 12, 2009 at 12:08 am

plz help

how to capture desktop image

Reply

10 hari May 14, 2009 at 2:14 am

this is exactly what i need.

kudos for you and many thanks!

Reply

11 Andrew May 18, 2009 at 10:39 am

Nice and simple – great.

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

Andrew.

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: