1 Reply Latest reply on Mar 25, 2011 4:42 AM by JoshBeall

    Taking an image snapshot--how do I turn transparent background to white?

    JoshBeall Level 1

      Hi All,

       

      I'm doing the following to grab a snapshot of a component and encode it to PNG:

       

      var tBitmapData:BitmapData = mx.graphics.ImageSnapshot.captureBitmapData(confirmationComponent);

      var tEnc:IImageEncoder = new PNGEncoder();

      var tBA:ByteArray = tEnc.encode(tBitmapData:BitmapData );

       

      This works great and gives me the bytes of a PNG that has everything in displayed by confirmationComponent.  However, everything that's represented on the screen as being white is instead transparent in the PNG.  I would like it to be white.

       

      I thought I would try encoding to a JPEG, just to see what would happen.  All I did was swap out PNGEncoder() with JPEGEncoder().  In that case, the transparent regions of the image were replaced with black.

       

      Any advice on how I could replace transparent in that PNG with white?  I've been searching on this and so far have not come up with a solution...

       

        -Josh

        • 1. Re: Taking an image snapshot--how do I turn transparent background to white?
          JoshBeall Level 1

          Here's what I've figured out:

           

           

          // First get a snapshot as BitmapData that will have transparency
          var rawBitmapData:BitmapData = mx.graphics.ImageSnapshot.captureBitmapData (confirmationComponent);
          // Now draw a new bitmap, the same width and height as the old bitmap, with no transparency, and filled with white
          var trueBitmapData:BitmapData = new BitmapData(rawBitmapData.width,rawBitmapData.height,false,0xFFFFFFFF);
          // Then, draw the first bitmap onto the second bitmap
          trueBitmapData.draw(rawBitmapData);
          
          

           

           

           

          Is that the right way to go about it?  Is there a better way?  All I really want to is to get a white background, rather than a transparent background, on my image.