我正在尝试使用 Image
类的方法 FromStream
来设置使用网络流的 tcp 连接接收的图像。
这里有两个方面的代码:
服务器:
TcpListener server = new TcpListener(IPAddress.Any, 34567);
server.Start();
TcpClient client = server.AcceptTcpClient();
byte[] buffer = new byte[256];
int n = 0;
Console.WriteLine("{0}, {1}", Encoding.ASCII.GetString(buffer, 0, n), client.Client.RemoteEndPoint);
byte[] img = File.ReadAllBytes(@"C:\Users\Luca\Desktop\video grest\8599929-nessun-segnale.jpg");
client.Client.Send(img);
客户:
TcpClient tcp = new TcpClient("127.0.0.1", 34567);
NetworkStream stream = tcp.GetStream();
MessageBox.Show("fatto");
pictureBox1.Image = Image.FromStream(stream);
问题是客户端程序在最后一条指令处停止并且没有继续前进,如果关闭服务器(甚至 30 分钟后)它说连接已关闭但图片框没有显示任何内容.. . 为什么?
提前致谢。
最佳答案
我认为你的客户端代码
pictureBox1.Image = Image.FromStream(stream);
期望流在处理图像之前结束。
所以你应该关闭套接字服务器端使用:
client.Close();
就在:
client.Client.Send(img);
关于c# - Image.FromStream 使用 networkStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16965634/