Posted by: coldfusionflex | March 16, 2008

DragBitmap.as

Christophe Coenraets (http://coenraets.org/) put together a pretty kewl class to drag a bitmap from an AIR app to the desktop. Unfortunately the classes changed between Beta3 and the final release and the version he has on his site no longer works. This version works with the current release.

It’s called like this:

<utilities:DragBitmap displayObject=”{theObject}/>

The package itself:

package {

	import com.adobe.images.JPGEncoder;

	import flash.desktop.Clipboard;
	import flash.desktop.ClipboardFormats;
	import flash.desktop.NativeDragManager;
	import flash.desktop.NativeDragOptions;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;

	import mx.formatters.DateFormatter;

	public class DragBitmap
	{
		private var _displayObject:DisplayObject;
		private var dateFormatter:DateFormatter;

		public var dragIcon:BitmapData;

		public function DragBitmap()
		{
			var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
				function ():void
				{
					dragIcon = Bitmap(loader.content).bitmapData;
				}
			);
			loader.load(new URLRequest("assets/icons/icon_image.png"));
			dateFormatter = new DateFormatter();
			dateFormatter.formatString = "YYYY-MM-DD-HH-NN-SS";
		} 

		public function set displayObject(displayObject:DisplayObject):void
		{
			_displayObject = displayObject;
			_displayObject.addEventListener(MouseEvent.MOUSE_MOVE, startDragging);
		}

		private function startDragging(event:MouseEvent):void
		{
			if (!event.buttonDown)
			{
				return;
			}

			var options:NativeDragOptions = new NativeDragOptions();
			options.allowCopy = true;
			options.allowLink = true;
			options.allowMove = false;

			var data:Clipboard = new Clipboard();
			data.setData(ClipboardFormats.BITMAP_FORMAT, getBitmapData(), false);
			data.setData(ClipboardFormats.FILE_LIST_FORMAT,[createTempJPG()]);

			NativeDragManager.doDrag(_displayObject as InteractiveObject, data, dragIcon, null);
		}

		private function getBitmapData():BitmapData
		{
			var bd:BitmapData = new BitmapData(_displayObject.width, _displayObject.height);
			bd.draw(_displayObject);
			return bd;
		}

		private function createTempJPG():File
		{
			var file:File = File.createTempDirectory().resolvePath("data-"+dateFormatter.format(new Date())+".jpg");
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.WRITE);
			fileStream.writeBytes(encodeJPG());
			fileStream.close();
			return file;
		}

		private function encodeJPG():ByteArray
		{
			var jpgEncoder:JPGEncoder = new JPGEncoder();
			var bytes:ByteArray = jpgEncoder.encode(getBitmapData());
			return bytes;
		}

	}
}

Leave a response

Your response:

Categories