在 C# 中从 Windows Mobile 中的流获取图像
我正在尝试从服务器接收图像并希望将其显示到 WM 应用程序中的图片框中。我成功接收图像流,但找不到任何方法将其显示到 PictureBox 中。在Windows程序中,我们在Image类中有一个方法,即FromStream(Image.FromStream),但该函数在Compact Framework 3.5中不可用。我还尝试了以下代码来执行此操作:
private void button1_Click(object sender, EventArgs e)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress IP = IPAddress.Parse("192.168.1.2");
IPEndPoint IPE = new IPEndPoint(IP, 4321);
s.Connect(IPE);
byte[] buffer = new byte[55296];
s.Receive(buffer, buffer.Length, SocketFlags.None);
MemoryStream ms = new MemoryStream(buffer);
Image im = new Bitmap(ms); //EXCEPTION
pictureBox1.Image = im;
}
但它给出了异常。没有提供有关异常的详细信息,VS 仅显示一个带有文本“Exception”的对话框。
I am trying to receive the image form Server and want to display it into a Picturebox in WM Application. I am successfully receiving the Image Stream and I don’t find any way to display it into a PictureBox. In windows program we have a method in Image class that is FromStream (Image.FromStream) but this function is not available in Compact Framework 3.5. I also tried the following code to do so:
private void button1_Click(object sender, EventArgs e)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress IP = IPAddress.Parse("192.168.1.2");
IPEndPoint IPE = new IPEndPoint(IP, 4321);
s.Connect(IPE);
byte[] buffer = new byte[55296];
s.Receive(buffer, buffer.Length, SocketFlags.None);
MemoryStream ms = new MemoryStream(buffer);
Image im = new Bitmap(ms); //EXCEPTION
pictureBox1.Image = im;
}
But it gives an Exception. No detail is provided with the exception and VS is only displaying a dialogbox with the text "Exception".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的图像大小是否小于缓冲区的大小?如果不是所有多余的数据都会丢失并抛出异常。
您也可以尝试不指定缓冲区长度。
Does your image size is less than the size of the buffer? If not all the excess data is lost and an exception is thrown.
Also could you try without the buffer length specified.
您必须检查套接字的 Receive 方法的返回值才能确定
您的位图缓冲区有多大:
希望这有帮助。
You do have to check the return value of the socket's Receive method in order to determine
how large your bitmap buffer is:
Hope, this helps.