Taking components’ snapshots with ImageSnapshot class

August 7th, 2010 by Piotr Wierzgała Leave a reply »

The ImageSnapshot is a static class that provides methods to take snapshots of flex components and optionally convert them into a base64 encoded string.

Table of contents

Application

The application below lets you check how the ColorTransform class works. The application’s source is available under right mouse button context menu („view source”).

ImageSnapshot methods

The ImageSnapshot class offers two static methods to grab comonents’ snapshots: captureBitmapData and captureImage.

captureBitmapData

The captureBitmapData method is used to get a raw snapshot of a UI component as BitmapData. If snapshot dimensions passes the maximum bitmap dimension defined in the ImageSnapshot’s MAX_BITMAP_DIMENSION const (2880 pixels) then snapshot is scaled down. At the end captured bitmap is drawn on bitmapData object with draw method. The captureBitmapData takes following parameters:

  • source: An object that implements the flash.display.IBitmapDrawable interface which in practice is every object that inherits from DisplayObject class.
  • matrix: A Matrix object used to scale, rotate, or translate the coordinates of the captured bitmap.
  • colorTransform: A ColorTransform object that you use to adjust the color values of the bitmap. For more information you can go to “Adjusting color values with ColorTransform class” post on this blog.
  • blendMode: A string value, from the flash.display.BlendMode class, specifying the blend mode to be applied to the resulting bitmap. I tried to find out how to modify a captured image with this parameter but I didn’t get any satisfying results. If you have information how to use this parameter I’ll thankfull if you contatct me.
  • clipRect: A Rectangle object that defines the area of the source object to draw. If you do not supply this value, no clipping occurs and the entire source object is drawn.
  • smoothing: A Boolean value that determines whether a BitmapData object is smoothed when scaled.

captureImage

The captureImage method is used to get a snapshot of a component as it is done by captureBitmapData method but it also lets you specify snapshot’s resolution (in dpi) and encoding format (PNG, JPEG). This method allows you to bypass 2880 px bitmap limitation by the means of stitching alghoritm that connects limitated bitmapData objects into one byteArray.

  • source: An object that implements the flash.display.IBitmapDrawable interface.
  • dpi: The resolution in dots per inch. If a resolution is not provided, the current on-screen resolution is used by default. The current on-screen resolution is stored in Capabilities.screenDPI variable.
  • encoder: The image format used to encode the raw bitmap. The two encoders are PNGEncoder and JPEGEncoder. If an encoder is not provided, the default is PNGEncoder.
  • scaleLimited: A Boolean value that if is set to true source object will be scaled down to 2880 pixels, otherwise smaller snapshot windows will be taken and stitched together to capture a larger image. The default is true.

References

Advertisement

Leave a Reply

Flexmaniaks on Facebook