Hello,
I'm making a networked client/server program (with a reverse connection, i.e. server connects to the client). I'm wanting to take a screenshot(server side) and transfer this file over to the client so that it can be saved.
Now before I go any further, the code I'm about to disclose works perfectly when running the server/client on the same machine. However, if I try to run it over a network I get a several rows of pixels in the saved screenshot file (the top), but nothing more - also the file size is never larger than 4kb where as it's usually around 300-400kb when working correctly.
Client Side - Receiving
byte[] buffer = new byte[1024];
int numberOfBytesRead = 0;
MemoryStream receivedData = new MemoryStream();
do
{
numberOfBytesRead = stream.Read(buffer, 0, buffer.Length); //Read from network stream
receivedData.Write(buffer, 0, buffer.Length); //Write to memory stream
} while (stream.DataAvailable);
File.WriteAllBytes(@"C:\file.png", receivedData.ToArray());
Server Side - Sending
//Take screenshot and save to memory
MemoryStream screenshotMemory = new MemoryStream();
TakeScreenshot().Save(screenshotMemory, ImageFormat.Png); //TakeScreenshot is my own method that returns a Bitmap
clientConnection.WriteLine("SEND"); //Just my own message code to let the client know a file is being sent
clientConnection.SendData(screenshotMemory.ToArray());
Edit: Woops, also clientConnection.SendData() is just a method for:
m_Client.GetStream().Write(data, 0, data.Length); //Where m_Client is the TcpClient
Hope that makes sense. If you need any more information please ask, as it's starting to get really annoying :(
Thanks!