views:

162

answers:

2
         var bmd:BitmapData = ImageSnapshot.captureBitmapData(someSprite);
         trace("bmd size "+getSize(bmd));
         var bounds:Rectangle = new Rectangle(0, 0, bmd.width, bmd.height);
     var snapshot:ImageSnapshot = new ImageSnapshot(0,0,bmd.getPixels(bounds));

         //var snapshot:ImageSnapshot = ImageSnapshot.captureImage(someSprite);
     var file:FileReference = new FileReference();
     file.save(snapshot.data,'abc.png');

In the above code after saving the file, when I try to open it, I get "This is not a valid bitmap file". I have tried 2-3 different viewers.

+2  A: 

The constructor of the ImageSnapshot method takes width and height as its first two arguments. You are passing zeros. Change those to their actual values.

var snapshot:ImageSnapshot = new ImageSnapshot(bmd.width, bmd.height, 
        bmd.getPixels(bounds));
Amarghosh
It looks like this wasn't the original problem (since they tried ImageSnapshot.captureImage first), but it certainly will stop them now.
Michael Brewer-Davis
same problem still :(
dta
@Michael, ImageSnapshot.captureImage is working fine. I used captureBitmapData since I wanted to later use a clipping rectangle and a scaling matrix.
dta
@dta The fourth parameter to the constructor is the encoder, and it is `null` by default. Try passing `PNGEncoder`.
Amarghosh
@amar fourth parameter is contentType(Flex SDK 3.5), which I tried passing as "image/png". Same effect.
dta
@dta How about `"PNGEncoder"`
Amarghosh
A: 

To extend Amarghosh's answer, look to the constructor of ImageSnapshot

ImageSnapshot(width:int, height:int, data:ByteArray, contentType:String)

The data field isn't expecting BitmapData pixel data (bmp.getPixels), it's expecting data encoded in the given contentType. So you could do:

var encoder:PNGEncoder = new PNGEncoder();
var bytes:ByteArray = encoder.encode(bmp);
new ImageSnapshot(width, height, bytes, encoder.contentType);

Once you have to encode it yourself anyway, you should probably do away with the second ImageSnapshot reference and use:

new FileReference().save(bytes, "abc.png");
Michael Brewer-Davis