Exporting Visible Elements in Flash

Exporting Visible Elements in Flash

Taking screenshots on your flash application is easy as flash supports it out of the box. I have written a simple exporter class to do this:
package com.utils
{
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.utils.ByteArray;
	
	import mx.core.UIComponent;
	import mx.graphics.codec.PNGEncoder;
	import mx.utils.Base64Encoder;

	public class ImageExport
	{
		static public function export(elements:Array):Array
		{
			var dataArray:Array = [];
			for each(var element:UIComponent in elements)
			{
				dataArray.push(_getBitmapData(element));
			}

			return dataArray
		}

		static protected function _getBitmapData(target:UIComponent):String
		{
			var bd:BitmapData = new BitmapData(target.width, target.height);
			var m:Matrix = new Matrix();
			bd.draw(target, m);
			
			var encoder:PNGEncoder = new PNGEncoder;
			var png:ByteArray = encoder.encode(bd);

			var be:Base64Encoder = new Base64Encoder();
			be.encodeBytes(png);
			return be.flush();
		}
	}
}
By calling the “export” function, it will return an array of encoded image strings and ready for you to json_encode it and then send to the server and allow user to download the images. For PHP applications, you will need to json_decode the array and then base64_decode each of the image data before saving them into files or push to the browser content. Happy Flex and PHPing.

Leave a Reply

Your email address will not be published.

My new Snowflake Blog is now live. I will not be updating this blog anymore but will continue with new contents in the Snowflake world!