如何在 WPF 中显示渐进式 JPEG?
如何在从 Web URL 加载时显示渐进式 JPEG?我正在尝试在 WPF 中的图像控件中显示 Google 地图图像,但我想保留图像作为渐进式 JPG 的优势。
如何在 WPF 中加载渐进式 JPG?
Image imgMap;
BitmapImage mapLoader = new BitmapImage();
mapLoader.BeginInit();
mapLoader.UriSource = new Uri(URL);
mapLoader.EndInit();
imgMap.Source = mapLoader;
目前我就靠这个凑合着。它只会在完全加载后显示图像。我想逐步展示它。
How to display a progressive JPEG as it loads from a web URL? I am trying to display a Google Maps image in a image control in WPF, but I want to keep the advantage of the image being a progressive JPG.
How to load a progressive JPG in WPF?
Image imgMap;
BitmapImage mapLoader = new BitmapImage();
mapLoader.BeginInit();
mapLoader.UriSource = new Uri(URL);
mapLoader.EndInit();
imgMap.Source = mapLoader;
Currently, I make do with this. It will only shows the image after it loads completely. I want to show it progressively.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个非常基本的示例。我确信还有优化的空间,您可以从中创建一个单独的类来处理大量请求,但至少它可以工作,并且您可以根据您的需要塑造它。另请注意,每次我们报告进度时,此示例都会创建一个图像,您应该避免它!大约每 5% 左右制作一张图像,以避免较大的开销。
Xaml:
隐藏代码:
A very basic sample. Im sure there are room for optimizations, and you can do a separate class from it that can handle numerous request, but at least its working, and you can shape it for your needs. Also note that this sample creates an image every time that we report a progress, you should avoid it! Do an image about every 5% or so to avoid a big overhead.
Xaml:
Code-behind:
这似乎确实是 Image 控件的一个缺点。也许您可以创建一个继承自 Image 的 StreamImage,在构造函数中获取一个流,从流中读取后台的字节,计算出何时有足够的字节,用到目前为止读取的字节构造一个内部“模糊图像”并呈现该图像迭代直到拥有所有字节。您必须了解渐进式 JPEG 的字节是如何发送的——我认为这并不简单。
This does seem to be a shortcoming of the Image control. Maybe you could create a StreamImage that inherits from Image, takes a stream in the constructor, reads bytes in the background from the stream, figures out when it has enough, constructs an internal "fuzzy image" with the bytes read so far and renders that iteratively until it has all the bytes. You'd have to understand how bytes of a progressive JPEG are sent--I don't imagine it is simple.