Transferring image via socket, the image turns black

0

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);

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 images 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 prints a valid image, the problem is receiving.

    
asked by anonymous 31.08.2016 / 21:22

1 answer

0

Solved! The server buffer was limited to 1024 bytes, so when the message was sent, the first few lines of its array fell within 1024 bytes:

    
02.09.2016 / 09:57