Image.FromStream使用networkStream [英] Image.FromStream using networkStream

查看:112
本文介绍了Image.FromStream使用networkStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用类Image的FromStream方法来设置使用网络流通过tcp连接接收的图像。
这里有双方代码:

i'm trying to use the method FromStream of the class Image to set an image received by a tcp connection using a network stream. Here there are the two sides code:

服务器:

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分钟后),它说连接已关闭,但图片框没有显示任何内容......为什么?

The problem is that the client program stops on the last instruction and it doesn't go forward, and if close the server (even 30 minutes later) it says the connection was closed but the picturebox doesn't show anything... Why?

提前致谢。

推荐答案

我认为您的客户代码

pictureBox1.Image = Image.FromStream(stream);

期望在处理图像之前结束流。

is expecting for the stream to be ended before processing the image.

所以你应该使用以下方法关闭套接字服务器端:

So you should close the socket server-side using:

client.Close();

紧随其后:

client.Client.Send(img);

这篇关于Image.FromStream使用networkStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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