Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中使用套接字发送图像_C#_Image_Sockets_Send_.net Micro Framework - Fatal编程技术网

C# 在C中使用套接字发送图像

C# 在C中使用套接字发送图像,c#,image,sockets,send,.net-micro-framework,C#,Image,Sockets,Send,.net Micro Framework,我试图使用NETMF中的套接字将原始图像从服务器发送到客户端,但没有成功 这是我的服务器端代码,由于NETMF中的缓冲区大小限制,我使用chunks方法发送图像字节[]数组: if (serverSocket.IsConnected) { Byte[] bytesToSend = capturedata; Byte[] outboundBuffer = new

我试图使用NETMF中的套接字将原始图像从服务器发送到客户端,但没有成功

这是我的服务器端代码,由于NETMF中的缓冲区大小限制,我使用chunks方法发送图像字节[]数组:

 if (serverSocket.IsConnected)
                    {
                        Byte[] bytesToSend = capturedata;
                        Byte[] outboundBuffer = new byte[512];


                        int incomingOffset = 0;

                        while (incomingOffset < bytesToSend.Length)
                        {
                            int length = System.Math.Min(outboundBuffer.Length, bytesToSend.Length - incomingOffset);                                                            
                            Array.Copy(bytesToSend, incomingOffset, outboundBuffer, 0, length);
                            incomingOffset += length;

                            // Transmit outbound buffer
                            serverSocket.SendBinary(outboundBuffer);

                        }
这是我的客户端代码。我正在尝试从memorystream中准备图像并在picturebox中显示:

  public static void ReadCallback(IAsyncResult ar)
    {


        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state = (StateObject)ar.AsyncState;
        handler = state.workSocket;

        // Read data from the client socket. 
        int bytesRead = handler.EndReceive(ar);

        if (bytesRead > 0)
        {

            memorystream.Write(state.buffer, 0, bytesRead);

            if (bytesRead<=0)
            {

                PICTUREBOX_CAPTURED_IMAGE.Image = new System.Drawing.Bitmap(memorystream);// Here is an ArgumentException

            }

            else
            {
                // Not all data received. Get more.
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReadCallback), state);
            }
        }
    }
我不知道这是怎么回事,但似乎服务器端的缓冲区并没有完全写入客户端的memorystream,因为Byte[]bytesToSend=capturedata;所有的时间大约是12540字节,但当我开始研究客户端内存流容量的这个值时,它大约是3500-4600字节。我想正因为如此,当我在这行代码中尝试从流生成位图图像时,我有一个参数异常:

 if (bytesRead<=0)
            {

                PICTUREBOX_CAPTURED_IMAGE.Image = new System.Drawing.Bitmap(memorystream);// Here is an ArgumentException

            }

请帮助我找出我做错了什么?

只是一种预感,请尝试将memorystream的位置设置为0,以便它从一开始就读取。memorystream.Position=0;就在您尝试创建/分配图像之前。这没有帮助。它抛出相同的参数,例外是重置流位置是一个好主意。还可以尝试使用File.writealBytesDump.jpg、memorystream.ToArray保存数据,并在此处使用jpg检查文件,不知道您使用的是什么格式。这可能会给你一个线索。参数异常是否也包含消息?谁知道这可能是一个跨线程操作失败。我试图写入字节,但无法打开jpg文件,出现了一些问题。请告诉我更多的步骤。EndReceive的返回值正好是刚才接收到的数据的长度。您不应该确认是否有更多数据。即使接收到长度大于0的数据,也可能是最后一个数据。但是你的代码不考虑它。