Search code examples
c#actionscript-3flashc#-4.0air

Sending a bitmapData over network - as3


I am trying to send a bitmapdata to the server(written in C#) from the client(written in as3). The server after receiving the bitmapData should add the image to a folder in a given location. I am sending the filename along with the bitmapData to the server. I am able to read the filename but I am unable to read the bitmapData on the server side. It keeps throwing "ArgumentExceptionOccured - Parameter is not valid" exception.

Could anyone tell me how to send an image from the client(as3) to the server(C#) please?

Client Side code:

function onScreenCaptureClick(event:MouseEvent):Void
{
var filename:String = "TEST123";
var myBitmapData:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
myBitmapData.draw(stage);

// Custom Network class which contains the 'openNetworkToken' method to send the image to the         server
Network.OpenNetworkToken("ADDIMAGE" + "|" + filename + "|" + myBitmapData);    
}

Server side code:

private void onAddImagesHandler(MessageEventArgs args)
  {
        if (args.IsTokenized)
        {
            System.Diagnostics.Debug.WriteLine("### CREATE MESSAGE RECEIVED ###");
            System.Diagnostics.Debug.WriteLine("Message: " + args.OriginalMessage);

            try
            {
                 string filename = args.Arguements[0];

                 Bitmap img = new Bitmap(args.Arguements[1]); // "ArgumentExceptionOccured - Parameter is not valid"
            }
            catch (Exception ex)
            {
            }
            args.SendResponse("ImageADDED", true);
        }

    }

Any help appreciated

Thanks, Vinu


Solution

  • You can convert your Bitmap to a PNG and send it Base64 encoded with this code:

    var byteArray:ByteArray = new ByteArray();
    myBitmapData.encode(new Rectangle(0,0,640,480), new flash.display.PNGEncoderOptions(), byteArray); 
    var encoder:Base64Encoder = new Base64Encoder();
    encoder.encodeBytes(byteArray);
    
    Network.OpenNetworkToken("ADDIMAGE" + "|" + filename + "|" + encoder.toString());
    

    There are much more efficient ways to transmit the data if you can change your Network class to suit. See https://stackoverflow.com/a/1438199/514087 for the basic outline of that approach.