RSS twitter

Posts tagged ‘flash actionscript load image’

How to load an image from PC in flash with actionscript 3

This works in flash CS4 and flash player 10 only, also this literally means loading an image from your computer into your flash application, you are not sending it to any server.

First thing you do is create a Movie Clip and name it “holder”, this is where we are going to load the external image. From your components panel (ctr+F7) drag and drop a simple button, name it “load_bt”. Then copy this code and place it on your first frame:

import fl.containers.*;
var loaderUI:UILoader = new UILoader();
function loadExternalImage(userFilesReference:FileReference){
	userFilesReference.addEventListener(Event.COMPLETE, onComplete);
	userFilesReference.load();
	function onComplete(event:Event){
		loaderUI.unload(); //cleanup
		loaderUI.loadBytes(userFilesReference.data);
		holder.addChild(loaderUI);
	}
}
load_bt.addEventListener(MouseEvent.CLICK, selectFiles);
var fileRef:FileReference = new FileReference();
fileRef.addEventListener(Event.SELECT, selectHandler);
function selectHandler(event:Event):void{
	loadExternalImage(fileRef)
}
function selectFiles(event:MouseEvent){
	fileRef.browse([new FileFilter("Images", "*.jpg;*.jpeg;*.gif;*.png")]);
}