0

I'm trying to make to add a simple desktop viewer to my application but I'm having problem sending the bitmap image to my server using socket class. I look over the net and all I find is to use stream and binaryformatter but it would change my whole network protocol. So I was hoping if someone here can help me.

Here is my code for getting bitmap image and my send function.

private Image getDesktopImage()
{
    Rectangle sb = Screen.PrimaryScreen.Bounds;
    Bitmap img = new Bitmap(sb.Width, sb.Height, PixelFormat.Format32bppArgb);
    using (Graphics g = Graphics.FromImage(img)) g.CopyFromScreen(sb.X, sb.Y, 0, 0, sb.Size, CopyPixelOperation.SourceCopy);
    return img;
}

public void sendData(string msg)
{
    byte[] sdata = Encoding.ASCII.GetBytes(msg);
    clientSocket.Send(sdata, 0, sdata.Length, 0);
}

and here is my function for receiving function

void clientHandle(object handle)
{
    int _handle = (int)handle;
    try
    {
        byte[] receiveByte = new byte[1028];
        int buffer = serverSocket[_handle].Receive(receiveByte, 0, receiveByte.Length, 0);
        Array.Resize(ref receiveByte, buffer);

        if (Encoding.ASCII.GetString(receiveByte) == "")
        {
            DisconnectedEvent(_handle);
        }
        ReceivedEvent(_handle, Encoding.ASCII.GetString(receiveByte));
        clientHandle(_handle);
    }
    catch
    {
        DisconnectedEvent(_handle);
    }
}

So my question is how can I convert the bitmap image so I can use my sendData function to send bitmap image. and then when i received it and convert it back to image. thanks

user3717030
  • 15
  • 1
  • 7
  • 1
    You do not have a protocol in place. Bitmap data also is not representable in an ASCII string. Please follow the tutorials you found, fixing your existing code is too broad for SO as it will require an entire rewrite. – CodeCaster Sep 08 '14 at 09:32
  • Convert bitmap image to array of byte and Send .On the other end Receive bytes and convert it into MemoryStream ,and further convert it into Bitmap using FromStream(...) – Charlie Sep 08 '14 at 09:44
  • Just convert the bitmap to a byte array and send it. The needed function can be found in [this SO question](http://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array). – Oliver Sep 08 '14 at 09:54

0 Answers0