Bitterblue
Bitterblue

Reputation: 14075

Save image with pure Flash?

Very simple: How do I save an image with pure Flash only?


Example code:

var bitmap: BitmapData = new BitmapData(100, 100, true, 0);
bitmap.draw(this);
var data: ByteArray = bitmap.getPixels(bitmap.rect);
var f: FileReference = new FileReference();
f.save(data, "test.bmp");

This saves me a file of 40,000 bytes (4 * 100 * 100). I checked it with hex editor and it is the pixels of my bitmap/sprite. Now I don't care what image type comes out of this. I just want it saved in a format that I can display with common image editors. I don't want to install extra stuff. Can I do it with Flash only, no AIR, no JPGEncoder, no nothing extra?

(This function is not for customers to use anyways.)

Upvotes: 0

Views: 219

Answers (2)

mitim
mitim

Reputation: 3201

You can still use PNGEncoder or JPGEncoder and still be "pure flash". If you're missing the libraries you can get them (and others) in the as3corelib swc library. Just include the swc in your library path.

If for some reason it's not pure enough, I suppose you could always go in to the source code of the library and copy out the specific PNGEncoder or JPGEncoder class and repurpose it for your uses.

Upvotes: 0

user2655904
user2655904

Reputation:

I found a solution. First off, you need to make sure that you are targeting a Flash Player with at least version 11.3, and compile the SWF with at least a version of 18 (or at least, 18 worked for me).

The BitmapData Class has its own method called encode which will handle the encoding work for you, either for JPEG or PNG. Here is how to do it:

var bitmap:BitmapData = new BitmapData(100,100,true,0);
bitmap.draw(this);
var data:ByteArray = new ByteArray();
var o:PNGEncoderOptions = new PNGEncoderOptions();
bitmap.encode(bitmap.rect, o, data);
var f:FileReference= new FileReference();
f.save(data, "test.png");

The third parameter of encode is the output Bytearray where the encoded Bytearray will get saved.

AS3 Documentation

Upvotes: 1

Related Questions