Reputation: 141
What I am trying to do is to transfer an image of the client's screen to the server. To do so, in the client side, I get the screen's image, convert it to byte array and send the byte array to the server over TCP sockets. In the server side, I convert the byte array from the client to image and put it in pictureBox.
Well, the result seems to be cool on the client side, but the picture I get on the server side is distorted:
The right picture is the client side (saves the screen and sends it to the server), and the server side is on the left. As you can see on the server's side, the picutre is not full.
I am using these codes to send an image, recieve the image and to convert (byte > image || image > byte):
private Image getScreen()
{
Size s = Screen.PrimaryScreen.Bounds.Size;
Bitmap b = new Bitmap(s.Height, s.Width);
Graphics g = Graphics.FromImage(b);
g.CopyFromScreen(0, 0, 0, 0, s);
return b;
}
private byte[] BmpToBytes(Image bmp)
{
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Jpeg);
byte[] bmpBytes = ms.GetBuffer();
bmp.Dispose();
ms.Close();
return bmpBytes;
}
private Image BytesToImg(byte[] bmpBytes)
{
MemoryStream ms = new MemoryStream(bmpBytes);
Image img = Image.FromStream(ms);
return img;
}
Client side:
private void SendScreenToServer()
{
while (true)
{
byte[] sendBytes = new byte[12500];
sendBytes = BmpToBytes(getScreen());
pictureBox1.Image = getScreen();
serverStream.Write(sendBytes, 0, sendBytes.Length);
serverStream.Flush();
}
}
Server side:
private void GetScreenFromClient()
{
while (true)
{
byte[] bytesFrom = new byte[12500];
networkStream.Read(bytesFrom, 0, clientSocket.ReceiveBufferSize);
try
{
pictureBox1.Image = BytesToImg(bytesFrom);
}
catch { }
}
}
Anyone has idea what am I doing wrong? or is there any other option to transfer image over sockets?
Upvotes: 2
Views: 2340
Reputation: 34188
it would be bad idea if you transfer whole image every time over the network rather first time you should send the whole image and from the next time you should send only the difference between previous and current image to server and on the server end you just apply the difference on the current image. you can search google to get good logic for sending difference between image over the network and apply the diff on the current image on server end. thanks
Upvotes: 0
Reputation: 116118
I'll make a guess blindly, since you haven't shown us the code you use for transfer images.
stream.Read(buffer,start,length)
does not guarantee that you will get length
bytes, instead it is upper limit not to overflow the buffer. You should check the return value which gives the number of bytes read.
EDIT
check what networkStream.Read(bytesFrom, 0, clientSocket.ReceiveBufferSize)
returns. it is probably less than 12500.
You may also need to send the image's size before sending the image to be able to know how many bytes to read from the stream exactly.
Upvotes: 2
Reputation: 182769
You forgot to design and implement a protocol.
networkStream.Read(bytesFrom, 0, clientSocket.ReceiveBufferSize);
This receives a bunch of bytes from a TCP data connection. The next thing you have to is process those bytes according to your protocol.
How is the receiver supposed to know when it has received the entire image? You have to decide how and then code that.
Upvotes: 1