0

For the past few hours I have been trying to clone an image in Flex (using the Spark Components, but also trying to convert between Bitmap and BitmapImage).

What I am trying exactly is to create a simple painting application which keeps track of each Brush-Stroke. As soon as the Image on the Canvas has changed, it is to be cloned and then the clone is to be put into the History-Panel on the bottom of the application.

Things I have tried include:

  • Using ObjectUtils.clone(Object)
  • Creating BitmapData from Image.content, then making it a Bitmap and simply display it (Image doesn't have a content field, it says)
  • Performing a byte-copy and others I could find on the internet, of course.

So basically, how does one clone an Image (Spark Image) in Flex 4.6?

Thank you very much!

-- Danny Nophut

2 Answers2

10

Instead of cloning you can get the image of the drawing and set the bitmap of the image as source to the history image, do some thing like this

private function getBitmapData( target:DisplayObject ) : BitmapData
{

   //target.width and target.height can also be replaced with a fixed number.
   var bd : BitmapData = new BitmapData( target.width, target.height );
   bd.draw( target );
   return bd;
}

In some case if the width and height of the target is not working you can use the getbounds method to get the bounds of the object and from the bounds take the width and height.

Triode
  • 11,309
  • 2
  • 38
  • 48
  • 1
    +1 from me. I didn't validate the code; but the concept is what I would have recommended. – JeffryHouser Mar 12 '12 at 17:00
  • Thank you very much, that seems to work :) Now all I do with that is to turn it into a Bitmap Object and display that? – Danny Nophut Mar 12 '12 at 17:06
  • 1
    @DannyNophut yes now you can just get the bitmap and set it for the image or else you can draw or add it over a sprite. – Triode Mar 12 '12 at 18:08
1

There's a clone function on a bitmapdata:

public class EZB2ParkObject extends Image implements IEZB2ParkObject
{

    public function clone():IEZB2ParkObject{
        var n:IEZB2ParkObject   = new EZB2ParkObject();
        n.id = this.id;
        n.source = new Bitmap(BitmapData(this.source.bitmapData).clone());
        n.dimensions = this.dimensions;
        n.assetId = this.assetId;
        return n;
    }
}
O Red
  • 145
  • 2
  • 13