POST 后将位图图像绑定到服务器的返回流

发布于 2024-12-03 16:04:46 字数 853 浏览 4 评论 0原文

我执行正常的 POST 方法,服务器返回一个流,这是一个位图图像,然后我想将 Image1 设置为我得到的该流:

        private void GetResponseCallback(IAsyncResult asynchronousResult)
    {

        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

        // End the operation
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        HttpStatusCode rcode = response.StatusCode;

        Stream stream = response.GetResponseStream();

        Dispatcher.BeginInvoke(() => 
        {
            var bi = new BitmapImage();
            bi.SetSource(stream);
            image1.Source = bi;
        });

        response.Close();
    }

这会出现错误:无法访问已处置的对象。 对象名称:“MS.Internal.InternalMemoryStream”。

我明白为什么会出现此错误,这是因为 Stream response.Close();我使用调度程序。如果我不使用调度程序,它将给出无效的跨线程。 如何将流设置为我的 image1?

I do a normal POST method and server return a stream, which is a bitmap image, then I want to set my Image1 to that stream that I got:

        private void GetResponseCallback(IAsyncResult asynchronousResult)
    {

        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

        // End the operation
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        HttpStatusCode rcode = response.StatusCode;

        Stream stream = response.GetResponseStream();

        Dispatcher.BeginInvoke(() => 
        {
            var bi = new BitmapImage();
            bi.SetSource(stream);
            image1.Source = bi;
        });

        response.Close();
    }

This give an error: Can not access a disposed object.
Object name: 'MS.Internal.InternalMemoryStream'.

I understand why I got this error, it's because the Stream response.Close(); and I use the Dispatcher. If I don't use Dispatcher it will give invalid-cross thread.
How do I set the stream to my image1?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你另情深 2024-12-10 16:04:46

为什么不创建一个回调和 UI 线程都可用的变量呢?

Why don't you create a variable, which is avaliable both the callback and the UI thread?

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