在 C# 中从 Windows Mobile 中的流获取图像

发布于 2024-11-30 09:04:06 字数 799 浏览 1 评论 0原文

我正在尝试从服务器接收图像并希望将其显示到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_蜘蛛 2024-12-07 09:04:06

您的图像大小是否小于缓冲区的大小?如果不是所有多余的数据都会丢失并抛出异常。

您也可以尝试不指定缓冲区长度。

        using (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];
            int rec = s.Receive(buffer, SocketFlags.None);
            using (MemoryStream ms = new MemoryStream(buffer, 0, rec))
            {
                Image im = new Bitmap(ms);
                pictureBox1.Image = im;
            }
        }

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.

        using (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];
            int rec = s.Receive(buffer, SocketFlags.None);
            using (MemoryStream ms = new MemoryStream(buffer, 0, rec))
            {
                Image im = new Bitmap(ms);
                pictureBox1.Image = im;
            }
        }
小ぇ时光︴ 2024-12-07 09:04:06

您必须检查套接字的 Receive 方法的返回值才能确定
您的位图缓冲区有多大:

    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];
    int rec = s.Receive(buffer, buffer.Length, SocketFlags.None);
    MemoryStream ms = new MemoryStream(buffer, 0, rec);
    Image im = new Bitmap(ms);
    pictureBox1.Image = im;

希望这有帮助。

You do have to check the return value of the socket's Receive method in order to determine
how large your bitmap buffer is:

    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];
    int rec = s.Receive(buffer, buffer.Length, SocketFlags.None);
    MemoryStream ms = new MemoryStream(buffer, 0, rec);
    Image im = new Bitmap(ms);
    pictureBox1.Image = im;

Hope, this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文