Transferring image via socket, image turns black

Asked

Viewed 114 times

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

Ready, the server receives the package everything normal: (remember that the first 4bytes of the package represent an integer that is the package 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 already tested the image before sending, it actually prints a valid image, the problem is on receiving.

  • 1

    Icarus, at the moment I can’t answer you, but I already put a question to you: Have you tried to do the conversion to byte and again to Image without sending through the socket? It is quite possible that the problem is in conversion and not in transmission itself. I await your return and the night I will have the time to answer if you still do not have the solution.

  • As you can see in the first code, I test before conversion and after conversion :/

  • Now I did a conversion just like the server at the end of the client’s sending code and it worked, the problem is on the server, it has something that limits byte transfer?

  • I did, it was the client’s receive buffer size, 1024... I had to put it at 900,000, so is there a way to make the buffer extend when the data is bigger than 1024?

  • 1

    Hello Icarus. Glad you solved it. But note that this site is not a forum. Ideally, you should answer your question yourself with the solution of this problem, in order to also help other people with the same difficulty in the future, and open a new question for your new question. In this case (the new question) prepare a [mcve], which will facilitate the understanding of who will answer and also facilitates you to explain the difficulty.

1 answer

0


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

  • Nice that you bothered to answer. But, I need to comment that the answer is low quality. Basically you mention what you mentioned in the comment. If you could put at least some piece of related code, with the before and after, you would be more sure to help someone with the same problem in the future as well. Otherwise, the answer helps your question to receive a vote to be closed as a mere "typo". Understand?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.