Bitmap and Sprite Conversion in ActionScript 3
I got tired of writing the Bitmap and Sprite Conversion code in AS3 multiple times and finally decided I’d write a small utility that would easily do this for me.
Usage:
Let’s say you have some some Sprite on the stage called _mcSprite that you want to become a bitmap, then you would do the following:
[as]var tmpBitmap:Bitmap = DisplayConverter.spriteToBitmap(_mcSprite);[/as]
DisplayConverter.bitmapToSprite() is also included. Both functions include an optional second parameter if you wish to enable smoothing.
Get the open source class here.
Update:
I just realized that the spriteToBitmap function did not maintain the transparency of the original. This is because the BitmapData constructor defaults the fillColor to opaque white instead of transparent. This can be fixed by setting the alpha byte of the ARGB hex code to “00″ instead of “FF”. For example:
[as]var bitmapData:BitmapData = new BitmapData(sprite.width, sprite.height, true, 0x00FFFFFF);[/as]
The class in the download link above has been updated with this issue resolved.
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 3.0 United States License.
Great Class, simple and to the point. Thank you!
Cheers for that!
Isn’t there problem using this if the registration point is centered ?
I have never had to deal with a differing registration point on any of my projects. But I suppose that would cause an issue. And the solution would be different depending on which direction you were applying the conversion.
Is your issue for going from a Sprite to a Bitmap? Then you could just use this code instead:
var r:Rectangle = sprite.getBounds(sprite);var bitmapData:BitmapData = new BitmapData(sprite.width, sprite.height, true, 0x00FFFFFF);
bitmapData.draw(sprite, new Matrix(1,0,0,1,-r.left,-r.top));
How about an SWF2PNG.EXE (forget using AS or flash or other crap for conversion), that would be a dedicated program to extract FRAME, SPRITE, and SHAPE, type vector graphics from SWF files and then export them as PNG image files (or at least 32bit BMP image files), complete with transparency (or alpha channel).
Anyone here want to write such a program? I’m personally interested in such, and none currently exists, so I’m requesting someone here with programming knowledge to write such a program for me. Thanks in advance.
@Animedude: There are programs that do that. If you just want the source you can use a decompiler, such as: http://www.sothink.com/product/flashdecompiler/index2.htm
If you have the source and just want png’s cut from it, you can use one of many tools such as this one: https://github.com/pixelrevision/texture_atlas_generator