Skip navigation
Venian
Currently Being Moderated

How to manipulate a bitmap loaded through FileReference.load() ?

Apr 11, 2012 8:19 AM

Hi there,

 

I'm loading a bitmap with FileReference.load() and than dump the byteArray into a Loader object and than add that loader object to stage.

The problem is that regardless if I load the loader into a Sprite or add it to the stage, neither the loader or the sprite don't have width or height. It's always 0. So I can't scale the image as I don't know it's dimensions.

 

Does anyone had this problem before and found a solution?

Thank you!

 
Replies
  • Currently Being Moderated
    Apr 11, 2012 8:32 AM   in reply to Venian

    this code works

     

     

    package
    {
              import flash.display.Loader;
              import flash.display.LoaderInfo;
              import flash.display.Sprite;
              import flash.events.Event;
              import flash.events.MouseEvent;
              import flash.net.FileFilter;
              import flash.net.FileReference;
              import flash.text.TextField;
              import flash.text.TextFieldAutoSize;
     
              public class Main extends Sprite
              {
                        private var fileRef:FileReference;
     
                        public function Main():void
                        {
                                  if (stage)
                                  {
                                            init();
                                  }
                                  else
                                  {
                                            addEventListener(Event.ADDED_TO_STAGE, init);
                                  }
                        }
     
                        public function init(e:Event = null):void
                        {
                                  removeEventListener(Event.ADDED_TO_STAGE, init);
     
                                  fileRef = new FileReference();
                                  stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
     
                                  var txt:TextField = new TextField();
                                  addChild(txt);
                                  txt.autoSize = TextFieldAutoSize.LEFT;
                                  txt.x = txt.y = 20;
                                  txt.text = "click anywhere to load an image file...";
                        }
     
                        private function onDown(evt:MouseEvent):void{
                                  fileRef.browse([new FileFilter("Images", "*.jpg;*.gif;*.png")]);
                                  fileRef.addEventListener(Event.SELECT, onSelected);
                                  stage.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
                        }
     
                        private function onSelected(evt:Event):void{
                                  fileRef.addEventListener(Event.COMPLETE, onLoaded);
                                  fileRef.load();
                                  fileRef.removeEventListener(Event.SELECT, onSelected);
                        }
     
                        private function onLoaded(evt:Event):void{
                                  var loader:Loader = new Loader();
                                  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
                                  loader.loadBytes(evt.target.data);
                                  addChild(loader);
                                  fileRef.removeEventListener(Event.COMPLETE, onLoaded);
                        }
     
                        private function onComplete(e:Event):void 
                        {
                                  var loaderInfo:LoaderInfo = e.target as LoaderInfo;
                                  loaderInfo.removeEventListener(Event.COMPLETE, onComplete);
     
                                  var loader:Loader = loaderInfo.loader;
                                  trace("loader size: ", loader.width, loader.height);
                        }
              }
    }
     
    
     
    |
    Mark as:
  • Currently Being Moderated
    Apr 11, 2012 8:35 AM   in reply to Venian

    sorry i initially left a bit off and have since updated the code.

     

    if you try and get the width and height of loader just after you call loadBytes you will get 0, 0

     

    you have to setup and eventlistener for Event.COMPLETE and when you receive that you can access the width and height

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 11, 2012 8:42 AM   in reply to Venian

    no worries

     

    the file reference loads the bytes into a ByteArray. Loader then needs to load the bytes from the ByteArray. It will be pretty quick but you still have to wait

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points