【发布时间】:2020-03-16 22:12:40
【问题描述】:
我正在尝试加载位图图像,同时等待它加载长达 10 秒。 为此,我需要确定图像何时完成下载。 因此,我正在检查“isDownloading”属性以确定图像是否确实正在下载。
代码如下:
Uri imageUri = new Uri(imageSource);
BitmapImage bitmapImage = new BitmapImage(imageUri);
if (bitmapImage.IsDownloading)
{
bitmapImage.DownloadCompleted += (s, e) => _autoResetEvent.Set();
var imageLoadingTimer = new Timer(10000);
imageLoadingTimer.Elapsed += (s, e) => _autoResetEvent.Set();
imageLoadingTimer.Start();
_autoResetEvent.WaitOne();
}
问题是,尽管根据 Fiddler 的说法,确实在 0.4 秒内下载并完成了下载,但 DownloadCompleted 事件永远不会触发,并且 isDownloading 属性始终为 true。
任何帮助将不胜感激 谢谢!
【问题讨论】:
-
在 fiddler 中如何下载图像?是 TCP 还是 HTTP?提琴手是否显示任何标题或连接关闭?图像是 base 64 流还是 GZIP? TCP 你无法判断接收到的二进制消息是否完成,除非在消息的开头给出字节数的连接关闭。
-
DownloadCompleted 事件在 UI 线程中执行,被 WaitOne 调用阻塞。
-
@jdweng 谢谢,提琴手打开一个 https 连接并下载整个图像 - 我可以在响应中看到,在 imageview 选项卡下,我试图下载的图片。我不确定你问的其他问题,你建议我接下来检查什么?
-
如果您使用的是阻塞的 http 响应,那么当响应返回时,您将获得整个消息。如果响应在 0.4 秒内返回,则不需要任何异步方法。
标签: c# wpf bitmapimage