Flex screenshot Snapshot and display implementation code _flex

Source: Internet
Author: User
Tags addchild
A snapshot of the specified screen, screenshot.
There are two classes bitmap and BitmapData under the Flash.display package in the Flex SDK. You can manipulate a picture in flex with two classes. There is a draw method in the BitmapData class the first parameter of this method is the source object for the screenshot, the object must implement the Ibitmapdrawable interface. And the Displayobject class implements this interface, Displayobject is the ancestor class of all visible controls, which means that all images and controls that can be seen in flex can be shot with a draw method.
The specific code is as follows:
Copy Code code as follows:

private var bmpimage:bitmap = null;
Private Function OnClick (): void
{
Create a picture of the same size and screenshot object
var bmpdata:bitmapdata = new BitmapData (source.width,source.height);
Take a snapshot screenshot where the second parameter matrix is used to transform the picture, such as rotation scaling.
If the picture does not require a transform, you can use either the new Matrix () or null.
Bmpdata.draw (Source,new Matrix ());
Creating Bitmap objects
Bmpimage = new Bitmap (bmpdata);
}

BitmapData object can not be directly displayed on the screen, so you need to encapsulate the BitmapData object into the bitmap, press the screenshot button, the panel's snapshot is generated, saved to the bitmap object.
Effect Picture:

Second, will capture the screenshot picture to display on the picture.
The next step is to display the bitmap object to the screen, which requires the help of the UIComponent class. Because the bitmap object is added directly to the canvas container, the run-time error occurs because the child object added in the container must be a subclass of UIComponent, and bitmap is not, so the UIComponent class is required to encapsulate one layer. The UIComponent object is then added to the canvas container so that the picture is displayed.
The code looks like this:
Copy Code code as follows:

Private Function OnShow (): void
{
Create a UIComponent object
var uic:uicomponent = new UIComponent ();
To add a bitmap object to the UIComponent object
Uic.addchild (Bmpimage);
Add a UIComponent object to the canvas
Showimage.addchild (UIC);
}

Click the screenshot button to take a screenshot, then press the display button to display the screenshot to the canvas. The effect is shown in the figure.

Three, the following is the complete code of this program:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml" layout= "Absolute"
Fontfamily= "Song Body" fontsize= "width=" "831" height= "448" >
<mx:panel id= "source" x= "y=", "width=" height= "263" layout= "Absolute" >
<mx:textinput x= "yi" y= "Yi"/>
<mx:textinput x= "y="/>
<mx:button x= "73.5" y= "158" label= "Cancel"/>
<mx:button x= "172.5" y= "158" label= "OK"/>
<mx:label x= "y=" text= "User name:"/>
<mx:label x= "y=" 108 "text=" Password: "/>
<mx:label x= "109" y= "text=" "User Login" fontweight= "bold"/>
</mx:Panel>
<mx:canvas id= "ShowImage" x= "427" "y=" width= "347" height= "292" >
</mx:Canvas>
<mx:label x= "124" y= "text=" "screenshot area"/>
<mx:label x= "562" y= "text=" "screenshot display area"/>
<mx:button x= "147" y= "394" label= "screenshot" click= "OnClick ()"/>
<mx:button x= "548" y= "394" label= "show" click= "OnShow ()"/>
<mx:Script>
<!--[cdata[
Import mx.core.UIComponent;
private var bmpimage:bitmap = null;
Private Function OnClick (): void
{
Create a picture of the same size and screenshot object
var bmpdata:bitmapdata = new BitmapData (source.width,source.height);
Take a snapshot screenshot where the second parameter matrix is used to transform the picture, such as rotation scaling.
If the picture does not require a transform, you can use either the new Matrix () or null.
Bmpdata.draw (Source,new Matrix ());
Creating Bitmap objects
Bmpimage = new Bitmap (bmpdata);
}
Private Function OnShow (): void
{
Create a UIComponent object
var uic:uicomponent = new UIComponent ();
To add a bitmap object to the UIComponent object
Uic.addchild (Bmpimage);
Add a UIComponent object to the canvas
Showimage.addchild (UIC);
}
]]-->
</mx:Script>
</mx:Application>

Four, summary.
Although you can use bitmap and BitmapData classes for screenshots and reality, but flex can not directly read and write files. The picture's preservation also needs the backstage program cooperation to complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.