简体   繁体   中英

Networkstream or Serializing sending images?

New to this, i have been using serializing for sending image data constantly. Meaning i save an image in a memorystream, and send it with serializing.

But i don´t understand if i am supposed to use Serializing or Networkstream. The only thing i am doing is sending images in a loop.

I have been trying to get a Networkstream to work, but it doesn´t go my way, so here is my code. It sens only 1 image for some reason, when i use serializing it goes constantly.

    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
        {
        }

    }

And here is the send:

    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
        {
        }  
    }

what PrintWindow(process, tcp.GetStream()); does is simply, screenshot an a window (process = the name of the process), then where to save it (tcp.GetStream()) which makes it send and stream it (at least i think so).

It does work a bit, if i disconnect and connect etc, it will send an image, but it´s pretty random i think, not sure why i have to do that, i want it to keep sending as long as the loop is active.

(This works perfectly with Serialization and MemoryStream (save the bitmap in a memorystream and use it in Serialization) ).

So this is like 2 questions, depending on the answer of the first.

What is faster for my purpose, (Sending an image), Serialization or NetworkStream? If NetworkStream is faster, how can i get it to work, what´s wrong with my code above.

Thanks

          if (tcp.Connected)
                {
                    MemoryStream ms = new MemoryStream();

                    PrintWindow(process, ms);
                    panel1.BackgroundImage = Image.FromStream(ms);

                }

This works, so it is getting connected, and the data is getting saved (changed to MemoryStream instead of NetworkStream for this).

You say you get an image when you disconnect. That makes sense because you're reading the image directly from the stream. Since the stream is a NetworkStream , it has no idea when it has to end.

My suggestion is to add the size of the image buffer before sending the actual image buffer when receiving/sending it using a BinaryReader , like this:

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
}

And when sending:

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);
}

I tweaked the code a bit, so just gonna write it here for other people. Now sure if it´s better or worse though. This is the sending part, i skip to convert to a byte[] and just use the MemoryStream.

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

And here for the receiving part:

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

Put everything in one command.

Of course the BinaryWriter/Reader are there, but i didn´t put them here, as i am just showing how i changed the code.

I used Angelo Geels Code above*

EDIT: for some reason it only seems to work in .bmp, not sure why, doesn´t make sense to me.

EDIT 2:

This can be solved with:

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

Think that´s the way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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