如何通过TCP连接发送图像 [英] how to send Image over tcp connection

查看:91
本文介绍了如何通过TCP连接发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我正在用C#做一个项目.我必须通过客户端和服务器之间的tcp连接发送多个图像.

我尝试使用BinaryFormatter序列化服务器上​​的映像,然后在客户端上反序列化,但是效果不是很好.

因此,现在我正尝试将Bitmap对象与BinaryWriter(在服务器上)和BinaryReader(在客户端上)一起使用.

在客户端上重新生成映像时,我还使用了memoryStream=ms和方法Image.fromStream(ms).

但是在此命令上,程序向我发送了无效参数的错误...为什么?这让我发疯了!!!!
请帮帮我!

客户代码

Hi everyone!
I''m doing a project in c#. I must send multiple images over tcp connection between a client and a server.

I''ve tried to use a BinaryFormatter for serialize the image on the server and deserialize it on the client, but it''s not so good.

So, now I''m trying to use a Bitmap object with a BinaryWriter (on server) and a BinaryReader(on client).

When I re-build the image on client, I use also a memoryStream=ms and the method Image.fromStream(ms).

But on this command, the program send me the error of invalid parameter...why? It made me crazy!!!!
Please help me!!

CLIENT CODE

private void StartReading()
       {
           while (true)
           {
               NetworkStream stream = Client.GetStream();
               //Bitmap bmp = new Bitmap(img);
               BinaryReader br = new BinaryReader(stream);
               byte[] datalen = new byte[4];
               br.Read(datalen, 0, 4);
               int len = BitConverter.ToInt32(datalen, 0);
               //Console.WriteLine("datalen---> {0}", len);
               //bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
               byte[] buffer = new byte[len];
               br.Read(buffer, 0, buffer.Length);
               //stream.Read(buffer, 0, Client.ReceiveBufferSize);
               MemoryStream ms = new MemoryStream(buffer, 0, buffer.Length);
               ms.Position = 0;
               Image img = Image.FromStream(ms);
               pictureBox.Image = bmp;


               stream.Close();
           }


服务器代码


SERVER CODE

     public void SendImage(Image img)
        {
            for (int i = 0; i < ClientList.Count; i++)
            {
                TcpClient tempClient = (TcpClient)ClientList[i];

                if (tempClient.Connected) //If the client is connected
                {
                    NetworkStream stream = tempClient.GetStream();
                    Bitmap bmp = new Bitmap(img);
                    BinaryWriter bw = new BinaryWriter(stream);
                    MemoryStream ms = new MemoryStream();
                    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] buffer = ms.ToArray();

                    bw.Write(buffer.Length);
                    bw.Write(buffer);
                    bw.Flush();
                    bw.Close();


        }
     }
}

推荐答案

但是在此命令下,程序向我发送了无效参数的错误信息……为什么?
到底哪个命令?也许是这样的:
But on this command, the program send me the error of invalid parameter...why?
Which command exactly? Perhaps the one that reads:
pictureBox.Image = bmp;


这篇关于如何通过TCP连接发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆