POST 后将位图图像绑定到服务器的返回流
我执行正常的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不创建一个回调和 UI 线程都可用的变量呢?
Why don't you create a variable, which is avaliable both the callback and the UI thread?