Image.FromStream(MemoryStream)中的参数无效

问题描述 投票:0回答:2

我试图通过网络流发送图像,我有一个sendData和Getdata函数,我总是得到一个无效的参数使用Image.FromStream函数

这是我的代码:我从屏幕上获取图片,然后将其转换为字节[]将其插入我通过networkStream发送的内存流。

    private void SendData()
    {
        StreamWriter swWriter = new StreamWriter(this._nsClient);
        // BinaryFormatter bfFormater = new BinaryFormatter();

        // this method
        lock (this._secLocker)
        {
            while (this._bShareScreen)
            {
                // Check if you need to send the screen
                if (this._bShareScreen)
                {
                    MemoryStream msStream = new MemoryStream();
                    this._imgScreenSend = new Bitmap(this._imgScreenSend.Width,   this._imgScreenSend.Height);
                    // Send an image code
                    swWriter.WriteLine(General.IMAGE);
                    swWriter.Flush();

                    // Copy image from screen
                    this._grGraphics.CopyFromScreen(0, 0, 0, 0, this._sizScreenSize);
                    this._imgScreenSend.Save(msStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    msStream.Seek(0, SeekOrigin.Begin);

                    // Create the pakage
                    byte[] btPackage = msStream.ToArray();

                    // Send its langth
                    swWriter.WriteLine(btPackage.Length.ToString());
                    swWriter.Flush();

                    // Send the package
                    _nsClient.Write(btPackage, 0, btPackage.Length);
                    _nsClient.Flush();
                }
            }
        }
    }


    private void ReciveData()
    {
        StreamReader srReader = new StreamReader(this._nsClient);
        string strMsgCode = String.Empty;
        bool    bContinue = true;
        //BinaryFormatter bfFormater = new BinaryFormatter();
        DataContractSerializer x = new DataContractSerializer(typeof(Image));
        // Lock this method
        lock (this._objLocker)
        {
            while (bContinue)
            {
                // Get the next msg
                strMsgCode = srReader.ReadLine();

                // Check code
                switch (strMsgCode)
                {
                    case (General.IMAGE):
                        {
                            // Read bytearray
                            int nSize = int.Parse(srReader.ReadLine().ToString());
                            byte[] btImageStream = new byte[nSize];
                            this._nsClient.Read(btImageStream, 0, nSize);

                            // Get the Stream
                            MemoryStream msImageStream = new MemoryStream(btImageStream, 0, btImageStream.Length);

                            // Set seek, so we read the image from the begining of the stream
                            msImageStream.Position = 0;

                            // Build the image from the stream
                            this._imgScreenImg = Image.FromStream(msImageStream); // Error Here
c# image memorystream networkstream
2个回答
1
投票

部分问题在于您正在使用WriteLine(),它在写入结束时添加了Environment.NewLine。当你只是在另一端调用Read()时,你没有正确处理该换行符。

你想要做的只是Write()到流,然后在另一端读回。

转换为字符串很奇怪。

在传输图像时,您正在做的是发送一个字节数组。您需要做的就是发送预期流的长度,然后发送图像本身,然后读取另一侧的长度和字节数组。

通过线路传输字节数组的最基本和最天真的方法是首先发送一个表示数组长度的整数,然后在接收端读取该长度。

一旦您现在知道要发送/接收多少数据,您就可以将数组作为原始字节数组发送到线路上,并读取您之前在另一侧确定的长度。

现在您已经拥有原始字节和大小,您可以将数组从缓冲区重建为有效的图像对象(或者您刚刚发送的任何其他二进制格式)。

另外,我不确定为什么DataContractSerializer存在。它是原始的二进制数据,你已经手动将它串行化为字节,所以这个东西没用。

使用套接字和流进行网络编程的一个基本问题是定义您的协议,因为接收端无法知道预期的内容或流何时结束。这就是为什么每个通用协议都有一个非常严格定义的数据包大小和布局,或者执行类似发送长度/数据对的原因,以便接收端知道该怎么做。

如果你实现了一个非常简单的协议,比如发送一个表示数组长度的整数并在接收端读取一个整数,你就完成了一半的目标。然后,发送者和接收者都对接下来会发生什么达成一致。然后,发送器在线路上准确地发送该字节数,接收器在线路上准确读取该字节数并认为读取完成。您现在拥有的是接收方原始字节数组的精确副本,然后您可以随意使用它,因为您首先知道该数据是什么。

如果你需要一个代码示例,我可以提供一个简单的例子,或者网上有很多例子。


1
投票

试着保持简短:Stream.Read函数(你使用它)返回一个int,它表示读取了多少字节,这将返回给你,这样你就可以验证是否收到了你需要的所有字节。就像是:

int byteCount=0;
while(byteCount < nSize)
{
      int read = this._nsClient.Read(btImageStream, byteCount, nSize-byteCount);
      byteCount += read;
}

这不是最好的代码

© www.soinside.com 2019 - 2024. All rights reserved.