samedi 25 avril 2015

Transmitting/receiving a bitmap with .NET Sockets C++/CLI


Im programming for my Schoolproject an Internet/Multiplayer based Drawing Game. So now i need to Program a PictureBox which is always actual with the Servers one.

First of all im using a .net TCP Client and Listener which already works(Im sending and receiving some strings). I have 2 static classes which represents the Server and the client.

My basic idea is to Convert the bmp from the PictureBox in a Byte[] transmitt it with a BinaryReader thru a NetworkStream.

On the Other Side the received Byte[] will be converted back to a bmp and goes into the PictureBox.

here my are my two Functions:

void Server::sendBMP(Bitmap^ bmp){
    array<Byte>^bytes = gcnew array<Byte>(256);
    BinaryWriter^ bw = gcnew BinaryWriter(stream);
    MemoryStream^ ms = gcnew MemoryStream(); 

    bmp->Save(ms,System::Drawing::Imaging::ImageFormat::Bmp); //Conversion from BMP to Byte[]
    bytes = ms->ToArray();  

    bw->Write(bytes);}


Bitmap^ Server::receiveBMP(void){
    array<Byte>^buffer = gcnew array<Byte>(10000);
    BinaryReader^ br = gcnew BinaryReader(stream);
    Bitmap^ bmp = gcnew Bitmap(1500,612);

    buffer = br->ReadBytes(10000);

    MemoryStream^ ms = gcnew MemoryStream(buffer); // Conversion From Byte[] to BMP
    bmp->FromStream(ms);

    return bmp;}

Im always getting a "System.ArgumentException" Error.

Im using synchronous TCP sockets. Is this the right Way im doing this?

Stack trace:

Server::receiveBMP() line 105 + 0x8 Bytes  
DrawMyThing::MyForm::streamIMG() line 2774 + 0x6 Bytes

line 105:

bmp->FromStream(ms);  //From my Server::receiveIMG()

line 2774:

private: static void streamIMG(void){
            while(1){    
             if(status==1){ //If Server
                     bitmap = Server::receiveBMP(); //line 2774

                 }else{
                     Client::sendBMP(bitmap);
                 }
            }
         }

BTW im calling this streamIMG function as a thread:

Thread^ imgstreamthread = gcnew Thread(gcnew ThreadStart(MyForm::streamIMG));
             imgstreamthread->Start();


Aucun commentaire:

Enregistrer un commentaire