Convert [Embed] Image to ByteArray

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);

    }
}
About these ads

Tags: , , , ,

2 Responses to “Convert [Embed] Image to ByteArray”

  1. Saurabh Seth Says:

    Hi fightermv,

    Can you help me with converting the embedded gif to Byte Array….
    Its Urgent……………………………………….

    Thanks in advance…………….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: