2 Replies Latest reply on Jan 12, 2010 1:01 PM by Francisc

    Convert image to BitmapData

    Francisc Level 3

      Hello!

       

      How can I convert an embedded PNG image to BitmapData type variable, please?

       

      Thank you!

        • 1. Re: Convert image to BitmapData
          ShardulSingh Level 3

          Hi pls find the code for the same.I have created the BitmapData variable from an embedded png image.And again a created a image from that Bitmap data.If u have any issue pls let me know.

           

          MainApplication.mxml

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
          <mx:Script>
              <![CDATA[
                  [Embed(source="img1.png")]
                  [Bindable]
                  public var imgClass:Class;

           

             
                  private var bitmapData : BitmapData;
                  private function getBitmapData( target : Bitmap ) : BitmapData
                      {
                          if(target != null)
                          {
                              bitmapData  = new BitmapData( target.width, target.height,true, 0x00000000 );
                              bitmapData.draw( target);
                          } 
                          return bitmapData;
                      }
                     
                      private function createImageFromBitmap():void
                      {
                          var data:BitmapData = getBitmapData(img.content as Bitmap);
                          var bitmap:Bitmap = new Bitmap(data);
                          imgFromBitmap.source = bitmap;
                      }

           

              ]]>
          </mx:Script>   
          <mx:Canvas width="100%" height="100%">
          <mx:VBox>
                  <mx:HBox width="100%" height="100%">
                          <mx:VBox width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
                              <mx:Image id="img" source="{imgClass}"/>
                          </mx:VBox>
                          <mx:VBox width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
                              <mx:Image id="imgFromBitmap"/>
                          </mx:VBox>
                  </mx:HBox>
                  <mx:HBox horizontalAlign="center" >
                      <mx:Button label="Create New Image From the BitmapData of the First" click="createImageFromBitmap()"/>
                  </mx:HBox>
          </mx:VBox>       
          </mx:Canvas>

           

             
          </mx:Application>

           

           

           

          with Regards,

           

          Shardul Singh Bartwal

          • 2. Re: Convert image to BitmapData
            Francisc Level 3

            Thank you, Shardul!

             

            However, what I needed was the opposite, create Bitmap from Image if possible.