C# 位图转换

C# 位图转换,c#,bytearray,C#,Bytearray,我试图将网络摄像头从客户端传输到服务器,但在将字节数组转换回服务器上的位图时遇到困难 代码如下: public void handlerThread() { Socket handlerSocket = (Socket)alSockets[alSockets.Count-1]; NetworkStream networkStream = new NetworkStream(handlerSocket); int thisRead=0; int blockS

我试图将网络摄像头从客户端传输到服务器,但在将字节数组转换回服务器上的位图时遇到困难

代码如下:

public void handlerThread()
{
    Socket handlerSocket = (Socket)alSockets[alSockets.Count-1];
    NetworkStream networkStream = new
    NetworkStream(handlerSocket);
    int thisRead=0;
    int blockSize=1024;
    Byte[] dataByte = new Byte[blockSize];
    lock(this)
    {
        // Only one process can access
        // the same file at any given time
        while(true)
        {
            thisRead=networkStream.Read(dataByte,0,blockSize);

            pictureBox1.Image = byteArrayToImage(dataByte);
            if (thisRead==0) break;
        }
        fileStream.Close();
    }
    lbConnections.Items.Add("File Written");
    handlerSocket = null;
}

public Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);  //here is my error
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}
在上面标记的点上,当试图转换回图像并崩溃时,我得到“参数无效”。关于我做错了什么有什么建议吗?

请注意这一点:

您可以创建扩展方法或删除传统方法的“this”。这看起来和你的代码一样,所以我想知道你是否有某种类型的编码或者与创建底层字节数组相关的其他问题

public static Image ToImage(this byte[] bytes)
{
    // You must keep the stream open for the lifetime of the Image.
    // Image disposal does clean up the stream.

    var stream = new MemoryStream(bytes);
    return Image.FromStream(stream);
}

看来我的问题是我的缓冲区太小了。不过首先需要做更多的测试。