Search code examples
apache-flexflash-builder

How to convert Image to ByteArray in Flex 4


I would like to write an image in pdf. I use AlivePDF and the method is pdf.addImageStream(image,...). The image is a ByteAray and I do not know how to convert my Image object in an ByteArray. I would appreciate if you can give me a suggestion.


Solution

  • 1.load the image:

    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, beginDraw);
    loader.load(new URLRequest(imgURL));
    

    2.get the bitmapData and convert to ByteArray:

    private function beginDraw(event:Event):void{
        var bitmap:Bitmap = loader.content as Bitmap;
        var rect:Rectangle = new Rectangle(0,0,bitmap.width. bitmap.height);
        var result:ByteArray = bitmap.bitmapData.getPixels(rect);
    }