1

What's the most resources efficient way to take a screenshot of display object in as3?

This is the code I am currently using:

public static function img(o:DisplayObject,width:int,height:int):ByteArray
    {
        var b:BitmapData = new BitmapData(width,height,true,0x000000);
        b.draw(o,new Matrix(o.width/width,0,0,o.height/height),null,null,null,true);
        return new JPGEncoder(35).encode(b);
    }

But it takes too much CPU power. I am okay if it will be processed more slowly, but without CPU utilization up to 60%.

Thanks.

Atm
  • 183
  • 1
  • 10

1 Answers1

1

It's JPEG encoding that takes most of the time, and not capturing of a displayobject to a BitmapData.

To achieve better performance (sacrificing its running time) you have to use some optimized version of standard JPEGEncoder class or/and its asynchronous version.

If you are not satisfied with the above, try googling for similar solutions: some guys out there have already solved the problem.

Note: you can also implement a couple of optimizations.

  • You don't need to create new Matrix instance every time. You can use one instance, calling Matrix.identity() before drawing. This will be of use if you perform this operation many times during one application session.
  • You don't need to create new JPEGEncoder instance every time. You can create one and hold it in some private static field (for example, create it on first call to img()).
Michael Antipin
  • 3,522
  • 17
  • 32
  • @Atern if someone gives you the right answer you need to accept that answer, and upvote too if you feel like it. You havn't accepted answers to either of your two questions. This will prevent people from helping you in the future. All that people get for taking the time to help you is the rep from your upvotes and accepts. –  Aug 11 '11 at 04:05
  • Vote Up requires 15 reputation – Atm Aug 11 '11 at 21:55
  • I meant I can not vote up unless I have at least 15 points reputation. I will up vote it as soon as I have. – Atm Aug 07 '12 at 08:28