Posts Tagged ‘[Embed]’

Convert [Embed] Image to ByteArray

January 25, 2010

I was searching for a way to convert a local image, which was saved in my “assets”-Folder to  ByteArray.

Why did I need this? Well – I had got a model, which saved images as ByteArrays (so I could update it with the FileReference-Class easily), but wanted to set a default image into the mode.

[Bindable]
public class MyModelClass
{
    [Embed(source="assets/myImage.png")]
    private var myImage:Class;

    public var imageToDisplay:ByteArray = new ByteArray();

    public function MyModelClass(){
        var bitmap:Bitmap = new myImage();
        var bitmapData:BitmapData = bitmap.bitmapData;
        bitmapData.draw(bitmap);

        var encoder:PNGEncoder = new PNGEncoder();

        imageToDisplay = encoder.encode(bitmapData);

    }
}