Transferring image via socket, the image turns black

My server sends a command to the client and the client sends a PrintScreen to the server using this Code:

Image printScreen = new Bitmap(monitorWidth, monitorHeight);
Graphics graphics = Graphics.FromImage(printScreen);
graphics.CopyFromScreen(0, 0, 0, 0, printScreen.Size);
printScreen.Save("Test1.jpg"); //Aqui sai uma linda imagem jpg
byte[] bufferTempTemp = imageToByteArray(printScreen);
byteArrayToImage(bufferTempTemp).Save("Test2.jpg"); //Aqui sai uma imagem jpg meio danificada mas tudo bem
byte[] bufferTemp = new byte[4 + bufferTempTemp.Length];
Buffer.BlockCopy(BitConverter.GetBytes(1), 0, bufferTemp, 0, 4);
Buffer.BlockCopy(bufferTempTemp, 0, bufferTemp, 4, bufferTempTemp.Length);
socketOfClient.Send(bufferTemp);

Ready, the server receives the packet all normal: (it is worth remembering that the first 4bytes of the packet represent an integer that is the packet ID, 1 means that it is an image coming)

byte[] bufferTemp = new byte[bufferReadOfServer.Length - 4];
Buffer.BlockCopy(bufferReadOfServer, 4, bufferTemp, 0, bufferReadOfServer.Length - 4);
Image image = byteArrayToImage(bufferTemp);
image.Save("Test3.jpg"); //Aqui sai uma imagem preta :/

Now when I go to see the image that was saved, it has 4Kb and is totally black, its size is exactly the size of my monitor, 1920 x 1080

Functions that convert bytes to image and vice versa:

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}

public Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}

I have already tested the image before sending, it actually printa a valid image, the problem is in the receipt.

Author: Ícaro Dantas, 2016-08-31

1 answers

Solved! The server buffer was limited to 1024 bytes, so when the message was sent, it barely fit the first rows of its array within 1024 bytes:)

 0
Author: Ícaro Dantas, 2016-09-02 07:57:11