Rachel Dockter
Rachel Dockter

Reputation: 986

Sending Image through TCP

im trying to send an image through tcp, It works about 50% of the time, the other 50% is just giving me a black image and if i send 2 in the space of like 3 seconds it crashes. does anyone know why? and how i can fix this

Client:

                    while ((i = stream.Read(datalength, 0, 4)) != 0)
                {

                    byte[] data = new byte[BitConverter.ToInt32(datalength, 0)];
                    stream.Read(data, 0, data.Length);
                    this.Invoke((MethodInvoker)delegate
                    {
                        try
                        {
                            Image Screenshot = byteArrayToImage(data);
                            pictureBox1.Image = Screenshot;
                        }
                        catch { }
                    });
                }

This is the function which converts the byte array to image

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

Server:

        Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        Graphics graphics = Graphics.FromImage(printscreen as Image);
        graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
                byte[] data = imageToByteArray(Image);
                stream = client.GetStream();
                int length = data.Length;
                byte[] datalength = new byte[4];
                datalength = BitConverter.GetBytes(length);
                stream.Write(datalength, 0, 4);
                stream.Write(data, 0, data.Length);

This is the function that converts the image to byte array

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

Upvotes: 1

Views: 1114

Answers (2)

Visual Vincent
Visual Vincent

Reputation: 18310

You have to make sure that you read the entire "packet". It is not certain that you receive everything just because you request it. The Read() method will return how many bytes have been read, store that and loop until you've received the right amount of bytes.

Replace your current Read() with this:

int bytesReceived = 0;
while(bytesReceived < data.Length)
{
    bytesReceived += stream.Read(data, bytesReceived, data.Length - bytesReceived);
}

This will read until your whole image has been received.

EDIT: Fixed a problem in code, thanks to Ivan's answer.

Upvotes: 1

user207421
user207421

Reputation: 310869

You're reading 1000 bytes when you should be reading four. You're reading and throwing away a large part of the image following the length word.

You're also apparently ignoring the result of read(), and assuming that it fills the buffer.

Upvotes: 0

Related Questions