Networkstream或Serializing正在发送图像

本文关键字:图像 Serializing Networkstream | 更新日期: 2023-09-27 18:26:37

对此,我一直在使用序列化来不断发送图像数据。这意味着我将图像保存在内存流中,并通过序列化发送。

但是我不明白我应该使用序列化还是Networkstream。我正在做的唯一一件事就是循环发送图像。

我一直在尝试让Networkstream工作,但它不符合我的要求,所以这是我的代码。由于某种原因,它只感应1个图像,当我使用序列化时,它会不断地进行。

    private void Initialize()
    {
        NetSerializer.Serializer.Initialize(new Type[] { typeof(MemoryStream) });
        tcplisten = new System.Net.Sockets.TcpListener(IPAddress.Any, 1700);
        tcplisten.Start();
    }
    private void Listen()
    {
        try
        {
            using (TcpClient tt1 = tcplisten.AcceptTcpClient())
            {
                tt1.NoDelay = true;
                while (checkBox1.Checked)
                {
                    panel1.BackgroundImage = Image.FromStream(tt1.GetStream());
                }
            }
        }
        catch
        {
        }
    }

这里是发送:

    private void Send()
    {
        try
        {                
            string process = Proccessname.Text;               
            using (TcpClient tcp = new TcpClient())
            {
                tcp.NoDelay = true;
                while (capcon == true)
                {
                    if (tcp.Connected)
                    {
                        PrintWindow(process, tcp.GetStream());
                    }
                    else
                    {
                        tcp.Connect(adress);
                    }
                }
            }
        }
        catch
        {
        }  
    }

什么PrintWindow(进程,tcp.GetStream());所做的只是,截屏一个窗口(进程=进程的名称),然后将其保存在哪里(tcp.GetStream()),使其发送并流式传输(至少我认为是这样)。

它确实有点工作,如果我断开连接等,它会发送一个图像,但我认为这是非常随机的,我不确定为什么我必须这样做,我希望它一直发送,只要循环是活动的。

(这与Serialization和MemoryStream非常配合(将位图保存在MemoryStream中并在Serialization中使用)。

这就像两个问题,取决于第一个问题的答案。

对于我的目的来说,什么更快,(发送图像),序列化还是NetworkStream?如果NetworkStream更快,我该如何让它工作,我上面的代码出了什么问题。

感谢

          if (tcp.Connected)
                {
                    MemoryStream ms = new MemoryStream();
                    PrintWindow(process, ms);
                    panel1.BackgroundImage = Image.FromStream(ms);
                }

这是有效的,所以它正在连接,并且数据正在保存(为此更改为MemoryStream而不是NetworkStream)。

Networkstream或Serializing正在发送图像

您说断开连接时会得到一个图像。这是有道理的,因为你直接从流中读取图像。由于流是NetworkStream,它不知道什么时候必须结束。

我的建议是在使用BinaryReader接收/发送实际图像缓冲区时,在发送实际图像缓冲器之前添加图像缓冲区的大小,如下所示:

BinaryReader reader = new BinaryReader(netStream);
while (true) {
  // read how big the image buffer is
  int ctBytes = reader.ReadInt32();
  // read the image buffer into a MemoryStream
  MemoryStream ms = new MemoryStream(reader.ReadBytes(ctBytes));
  // get the image from the MemoryStream
  Image img = Image.FromStream(ms);
  // ...do something with img
}

发送时:

BinaryWriter writer = new BinaryWriter(netStream);
while (someCondition) {
  // get the image
  Image img = SomeImage();
  // save the image to a MemoryStream
  MemoryStream ms = new MemoryStream();
  img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  // get the image buffer
  byte[] buffer = new byte[ms.Length];
  ms.Seek(0, SeekOrigin.Begin);
  ms.Read(buffer, 0, buffer.Length);
  // write the size of the image buffer
  writer.Write(buffer.Length);
  // write the actual buffer
  writer.Write(buffer);
}

我对代码做了一些调整,所以只想在这里为其他人写。现在确定它是好是坏。这是发送部分,我跳过转换为byte[],只使用MemoryStream。

                            using (MemoryStream ms = PrintWindow(process))
                        {
                            writer.Write(ms.Length);
                            writer.Write(ms.GetBuffer());
                        }

这里是接收部分:

panel1.BackgroundImage = Image.FromStream(new MemoryStream(reader.ReadBytes((Int32)reader.ReadInt64())));

把一切都放在一个命令里。

BinaryWriter/Reader当然在那里,但我没有把它们放在这里,因为我只是在展示我是如何更改代码的。

我使用了上面的Angelo Geels代码*

编辑:由于某种原因,它似乎只在.bmp中工作,不确定为什么,对我来说没有意义。

编辑2:

这可以通过以下方式解决:

writter.Write(ms.GetBuffer(),0,(int)ms.Length);

想想就是这样。