5 Replies Latest reply: Apr 11, 2012 8:42 AM by _spoboyle RSS

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

    Venian Community Member

      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!

        • 1. Re: How to manipulate a bitmap loaded through FileReference.load() ?
          _spoboyle Community Member

          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);
                              }
                    }
          }
          
          
          • 2. Re: How to manipulate a bitmap loaded through FileReference.load() ?
            Venian Community Member

            Hi,

             

            Thank you.

             

            But I know that method works, that's the method to load the image. The problem is that after you load it, it has 0 (zero) width and height even though the image displays. So I can't manipulate it as I can't find it's width and height.

            • 3. Re: How to manipulate a bitmap loaded through FileReference.load() ?
              _spoboyle Community Member

              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

              • 4. Re: How to manipulate a bitmap loaded through FileReference.load() ?
                Venian Community Member

                I see.

                 

                I didn't though that loadBytes was actually a loading process which of courses needs an onComplete event. It though it just dumps the bytes into the loader object since the image is already loaded into RAM.

                 

                Tnx mate!

                • 5. Re: How to manipulate a bitmap loaded through FileReference.load() ?
                  _spoboyle Community Member

                  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