简体   繁体   English

通过 TCP 发送图像

[英]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.我试图通过 tcp 发送图像,它大约有 50% 的时间工作,另外 50% 只是给我一个黑色图像,如果我在大约 3 秒的时间内发送 2 它会崩溃。 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();
    }

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. Read()方法将返回已读取的字节数,存储并循环,直到您收到正确数量的字节。

Replace your current Read() with this:将您当前的Read()替换为:

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 .编辑:修复了代码中的问题,感谢Ivan 的回答

You're reading 1000 bytes when you should be reading four.当您应该阅读四个字节时,您正在阅读 1000 个字节。 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.您显然也忽略了read()的结果,并假设它填满了缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM