5 Replies Latest reply on Sep 16, 2011 10:52 AM by bradygt

    Convert new Spark Image to Bitmap object

    William Spence Level 1

      I just started using the new Spark Image component and I love it, but there are not many examples yet that I can Google on various scenarios.  I have loaded a Spark Image and resized it, and I want to convert the newly scaled Image to a Bitmap object.  Using the old MX Image component, I used to convert an image to a Bitmap by using the content property like this after loading my image:


      var bitmap:Bitmap = Bitmap(image.content);


      The new Spark Image component no longer uses the content property, but I think there might be a sweet alternative.  The important thing here is that I have scaled this image down enormously and applied smoothing at the Best setting, so I would really like to get a Bitmap of the scaled/smoothed image.  I was thinking that I could use the cacheAsBitmap property as I think it creates a bitmap at your scaled dimensions and not the original dimensions (I think, please let me know if I am wrong).  The only problem is that I don't know how to go and get that cached Bitmap back so that I can do other things with it like convert it to BitmapData and crop it or save it to my harddrive.


      Any suggestions on how I can access that particular cached Bitmap?  Or any suggestions on alternative ways to convert this scaled Spark Image to a Bitmap Object?




        • 1. Re: Convert new Spark Image to Bitmap object
          blazejewicz Level 4

          Hi Bill,


          when you are done with loading/scaling/manipulating etc - get access to display object that is hosted within bitmap image and make bitmap copy of it, example:

          // display object is where content is drown/attached in ImageDisplay component

          var d:DisplayObject = image.imageDisplay.displayObject;

          // but it could not yet be ready

          if(d == null) return;

          // make copy of display objec drawing

          var bd:BitmapData = new BitmapData(d.width, d.height);


          var bmp:Bitmap = new Bitmap(bd);

          "bmp" should be exact copy of what you're seeing on screen (scaled/smoothed/etc) - *NOT* a copy of underlying bitmap content.


          (no: "cacheAsBitmap" has no direct context/relation in api that we developers use)





          • 2. Re: Convert new Spark Image to Bitmap object
            William Spence Level 1

            Thanks for the insight Peter, a real big help to me! A couple of additional questions:



            1. How do you load the new Spark Image control? With the MX Image, there was a load() method and you could load your URL and listen for the COMPLETE event to continue processing. The new Spark Image no longer has the load() method. Do I have to use an external loader and then set this loader as the source of the Image, or is there a more handy built in method I am just not seeing? (I actually need to load the image, not just point to a url using source )



            2. Is there an event that you can listen for to ensure that the display object that you mentioned in your post is loaded before you continue processing, or is it usually an immediate transfer?




            • 3. Re: Convert new Spark Image to Bitmap object
              blazejewicz Level 4

              Hi William,


              loading starts by setting "source" property to any of supported values:

              http://help.adobe.com/en_US/flex/using/WSc5cd04c102ae3e97-33ad5caa12c719dc7c8-8000.html#WS c5cd04c102ae3e97-33ad5caa12c719dc7c8-7ffe

              Component broadcast COMPLETE/READY. "READY" is always fired even if resource is embedded. COMPLETE only when resource is to be loaded (via internal Loader instance). I've not investigated on how to make sure that display object is there already - but READY event being fired should be good place to start I think (there could be delay though before object is attached to display list and lay out by component - so that could be yet avaiable when ready handler fires),




              • 4. Re: Convert new Spark Image to Bitmap object

                If anyone else stumbles on this, I was in the debug view when I noticed that there's actually a bitmapData property on the Spark image. And it returns a copy.

                So just new Bitmap(image.bitmapData)

                • 5. Re: Convert new Spark Image to Bitmap object

                  The problem that most people have with Image.bitmapData is that it is null for untrusted cross domain content.