0

I'm trying to pass a bitmap from an AIR app (server) to another (client), using the writeObject() method from the Socket class.

clientSocket.writeObject(myBitmap);// (var myBitmap:Bitmap)
clientSocket.flush();

My problem appears when I try to get the bitmap, on the client app. Using the method readObject(), I'm obtaining a generic Object, with all properties of the bitmap. I can't convert this object to bitmap by any way.

var receivedObject:* = socket.readObject();
trace(receivedObject);// [Object Object]
trace(receivedObject as Bitmap); // null

Some help please?

This kind of process is not well documented by Adobe.

Many thanks.

Miguel Lara
  • 183
  • 1
  • 12

1 Answers1

2

Bitmap class is a DisplayObject and cloning via ByteArray feature (with writeObject/readObject methods) don't work for DisplayObjects (check out this question for example how to work registerClassAlias() method for custom mxml components).

For this particular case you can write/read BitmapData object and the Rectangle property and restore Bitmap with it.

    var bd:BitmapData = myBitmap.bitmapData;
    var data:Object = {rect:bd.rect, bytes:bd.getPixels(bd.rect)}
    clientSocket.writeObject(data);

    var data:Object = socket.readObject();
    var bd2:BitmapData = new BitmapData(data.rect.width, data.rect.height);
    bd2.setPixels(bd2.rect, data.bytes);
    addChild(new Bitmap(bd2));
Community
  • 1
  • 1
fsbmain
  • 5,267
  • 2
  • 16
  • 23