Question

I am having trouble loading an image and displaying it on stage using the File API in as3. I am able to successfully load it in but when I put it on stage the image is just noise. I am assuming because I need to decode the PNG/JPG into Bitmap data somehow and I am doing it wrong? Here is my code:

public function browseForIcon(){
        var file:File = new File(); 
        file.addEventListener(Event.SELECT, onFileSelected); 
        file.browseForOpen("Select a an image"); 

    }

private function onFileSelected(event:Event):void {
         var stream:FileStream = new FileStream();
         stream.open(event.target as File, FileMode.READ);


        var bytes:ByteArray = new ByteArray();
        stream.readBytes(bytes);
        var img = new BitmapData(160,160);
        img.setPixels(new Rectangle(0,0,160,160),bytes);


        this.addChild(new Bitmap(img));
    }

}

THANKS!

Was it helpful?

Solution

One option is to use Loader.loadBytes(). If you're using Flex, you could also use an Image with source set to the File.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top