13 Replies Latest reply on Mar 4, 2010 1:19 PM by Flex harUI

    Loading bitmapdata from external image?

    Pete Heinz

      Hi everyone,

       

      I'm trying to retrieve the bitmapdata from an external image. In order to test my code, I convert the bitmapdata data a bitmap and I add it to the stage. Unfortunately I get a TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Bitmap@6b147e1 to mx.core.IUIComponent.

       

      Do you see why this error occurs?

       

      My code:

       

      import flash.display.BitmapData;

      import flash.display.Bitmap;

       

       

      private var bitmapData:BitmapData;

       

       

      private function init():void

      {

           var loader:Loader = new Loader();

           loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

           loader.load(new URLRequest("Beach.jpg"));

           var myImage:Bitmap = new Bitmap(bitmapData);

           addChild(myImage);    

      }

       

       

      private function onComplete (event:Event):void

      {

            bitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;

      }

       

      Thanks in advance

        • 1. Re: Loading bitmapdata from external image?
          jciglesias@uci.cu Level 1

          try to add the bitmap firt to a sprite object  as a container and then this to the stage

           

          let me know if that works

           

           

          import flash.display.BitmapData;

          import flash.display.Bitmap;

           

           

          private var bitmapData:BitmapData;

           

           

          private function init():void

          {

               var loader:Loader = new Loader();

               loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

               loader.load(new URLRequest("Beach.jpg"));

               var myImage:Bitmap = new Bitmap(bitmapData);

           

            //adding the image to a container (  sprite  )

           

              var container:Sprite = new Sprite();

              container.addChild( myImage );

           

             // then add the container to the stage

           

               addChild(container);    

          }

           

           

          private function onComplete (event:Event):void

          {

                bitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;

          }

          • 2. Re: Loading bitmapdata from external image?
            Pete Heinz Level 1

            It doesn't give any errors now. The image is still not displayed though.

            • 3. Re: Loading bitmapdata from external image?
              jciglesias@uci.cu Level 1

              sorry I miss one thing

               

              you need to show your image when the event onComplete is done

               

              if dosen't work now check your URLRequest

               

               

              import flash.display.BitmapData;

              import flash.display.Bitmap;

               

               

              private var bitmapData:BitmapData;

               

               

              private function init():void

              {

                   var loader:Loader = new Loader();

               

                   loader.load(new URLRequest("Beach.jpg"));

                   var myImage:Bitmap = new Bitmap(bitmapData);

               

               

                    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

               

               

               

              }

               

               

              private function onComplete (event:Event):void

              {

               

               

               

                    //adding the image to a container (  sprite  )

               

                    var container:Sprite = new Sprite();

                    container.addChild( myImage );

               

                   // adding the container to a stage

               

                   addChild(  container );

               

               

               

               

                    bitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;

              }

              • 4. Re: Loading bitmapdata from external image?
                Karl_Sigiscar_1971 Level 3

                Or you can simply use the Image component to load the picture, since it extends SWFLoader which extends UIComponent.

                 

                You can set the source property to a given URL and wait for the complete event.

                • 5. Re: Loading bitmapdata from external image?
                  Pete Heinz Level 1

                  It still doesn't display the image. The URL is correct.

                  This is my code:

                   

                  import flash.display.BitmapData;

                  import flash.display.Bitmap;

                   

                  private var bitmapData:BitmapData;

                  private var myImage:Bitmap;

                   

                   

                  private function init():void

                  {

                       var loader:Loader = new Loader();

                       loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

                       loader.load(new URLRequest("Beach.jpg"));

                       myImage = new Bitmap(bitmapData);

                       //adding the image to a container (  sprite  )

                        var container:Sprite = new Sprite();

                        container.addChild( myImage );

                        // adding the container to a stage

                        addChild(  container );

                  }

                   

                  private function onComplete (event:Event):void

                  {

                       bitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;

                   

                  }

                  • 6. Re: Loading bitmapdata from external image?
                    jciglesias@uci.cu Level 1

                    you need to put this lines into the event onComplete, because is when the external image is complete loaded

                    until that moment there's no image to show

                     

                     

                          //adding the image to a container (  sprite  )

                          var container:Sprite = new Sprite();

                          container.addChild( myImage );

                     

                          // adding the container to a stage

                          addChild(  container );

                    • 7. Re: Loading bitmapdata from external image?
                      Pete Heinz Level 1

                      I moved that code to the onComplete-handler but the image is still not displayed.

                       

                      My code:

                       

                      import flash.display.BitmapData;

                      import flash.display.Bitmap;

                       

                      private var bitmapData:BitmapData;

                      private var myImage:Bitmap;

                       

                      private function init():void

                      {

                           var loader:Loader = new Loader();

                           loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

                           loader.load(new URLRequest("Beach.jpg"));

                           myImage = new Bitmap(bitmapData);

                       

                      }

                       

                      private function onComplete (event:Event):void

                      {

                           bitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;

                           //adding the image to a container (  sprite  )

                            var container:Sprite = new Sprite();

                            container.addChild( myImage );

                            // adding the container to a stage

                            addChild(  container );

                       

                      }  

                      • 8. Re: Loading bitmapdata from external image?
                        jciglesias@uci.cu Level 1

                        sorry man,, your right,, it doesn't work, but know it does I just proved

                         

                         

                        import flash.display.Bitmap;

                         

                         

                         

                        private var bitmapData:BitmapData;

                         

                        private var myImage:Bitmap;

                         

                         

                         

                        private function init():void

                         

                        {

                         

                             var loader:Loader = new Loader();   

                         

                             loader.load(new URLRequest("C:\\Users\\Administrador\\Documents\\Flex Builder 3\\prueba\\src\\foto.jpg"));
                            
                             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

                         

                        }

                         

                         

                         

                        private function onComplete (event:Event):void

                         

                        {

                         

                             myImage = Bitmap(LoaderInfo(event.target).content);

                         

                             //adding the image to a container (  sprite  )

                         

                              var container:Sprite = new Sprite();

                         

                              container.addChild( myImage );

                         

                              // adding the container to a stage

                         

                              this.rawChildren.addChild(container);

                         

                         

                         

                        • 9. Re: Loading bitmapdata from external image?
                          Pete Heinz Level 1

                          I want to fill the bitmapData-variable with the data of the image. Do you know how to do that?

                          • 10. Re: Loading bitmapdata from external image?
                            jciglesias@uci.cu Level 1

                            BitMapData = myImage.bitmapdata;

                            • 11. Re: Loading bitmapdata from external image?
                              Pete Heinz Level 1

                              Now I want to convert the bitmapdata to a bitmap and display it. I get this error:

                              TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::BitmapData@727c629 to flash.display.Bitmap.

                               

                              Can you explain how to convert bitmapdata to bitmap and display it properly?

                               

                              My code:

                               

                              private var bitmapData:BitmapData;

                              private var myImage:Bitmap;

                               

                              private function init():void

                              {

                                   var loader:Loader = new Loader();

                                   loader.load(new URLRequest("Strand.jpg"));     

                                   loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

                              }

                               

                              private function onComplete (event:Event):void

                              {

                                   myImage = Bitmap(LoaderInfo(event.target).content);

                                   bitmapData = new BitmapData(320,240);

                                   bitmapData = myImage.bitmapData;

                                   //adding the image to a container (  sprite  )

                                   var container:Sprite = new Sprite();

                                   container.addChild( Bitmap(bitmapData) );

                                   // adding the container to a stage

                                   this.rawChildren.addChild(container);   

                              }

                              • 12. Re: Loading bitmapdata from external image?
                                jciglesias@uci.cu Level 1

                                you can't do that,, those classes work together,,

                                 

                                bitmap  has the propertys of the image and the other data of the image like pixel and those thing,,

                                 

                                when you wanna show an image you just  show the bitmap and only that,, that's why you cant do that convertion

                                • 13. Re: Loading bitmapdata from external image?
                                  Flex harUI Adobe Employee

                                  new Bitmap(bitmapData)