Speeding up image loading in WPF using thumbnails
Technorati Tags: wpf, thumbnails, image, performance, slow, BitmapImage
During a recent WPF session I needed to build a ListBox that showed a bunch of images loaded from an arbitrary directory. Thanks to WPF's data binding, this was trivial - I just needed to get a collection of objects that each had a property pointing to the full path of the image, and WPF would take care of all the loading/displaying. Something like this:
1: <ListView ItemsSource="{Binding}">
2: <ListView.ItemTemplate>
3: <DataTemplate>
4: <Image Source="{Binding Path=FullPath}" />
5: </DataTemplate>
6: </ListView.ItemTemplate>
7: </ListView>
The assumption here is that the DataContext for this window is set to a collection of "Photo" objects. The Photo class has a member called "FullPath" which is just a string with the full path of the photo on disk - this is what the Image.Source member expects.
This worked, but it didn't take long to see a major issue: With today's cameras, loading multiple 5+ megapixel images could take a while (not to mention the RAM requirements).
After a little digging, I found a solution. There exists a feature in the BitmapImage class that allows you to load an image but tell it to only load a thumbnail. To use this, you have to step out of the shrink-wrapped data binding world and insert a converter into the equation. Basically, this converter will take the above string with the full image path, it will load the image (as a thumbnail), and pass it back into the Image.Source parameter as aBitmapImage, which it's happy to consume.
First, let's look at this converter's code and how it loads the thumbnail:
1: public class UriToBitmapConverter : IValueConverter
2: {
3: public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
4: {
5: BitmapImage bi = new BitmapImage();
6: bi.BeginInit();
7: bi.DecodePixelWidth = 100;
8: bi.CacheOption = BitmapCacheOption.OnLoad;
9: bi.UriSource = new Uri( value.ToString() );
10: bi.EndInit();
11: return bi;
12: }
13:
14: public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
15: {
16: throw new Exception("The method or operation is not implemented.");
17: }
18: }
Notice line #7. That's the magic line which tells it how big of a thumbnail to load. The smaller the number, the quicker the load, the lower the quality. Notice also line #8 - this is there to force the image file to be closed after it's loaded. Without that, I found that my app couldn't write back to the image file since the ListBox still had it open.
Next, let's look at the XAML change to insert this converter into the mix. You'll need to create a resource for it:
1: <Window.Resources>
2: <local:UriToBitmapConverter x:Key="UriToBitmapConverter" />
3: </Window.Resources>
The "local:" namespace directive on line #2 is one I'd made sure to add to my main "Window" declaration, like this (line #4):
1: <Window x:Class="MyClass.Demo"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:local="clr-namespace:MyClass"
5: Title="Demo" Height="300" Width="300">
Lastly, use this new resource in the Image element so that FullPath (a string) gets pushed through the converter before Image.Source gets it:
1: <Image Source="{Binding Path=FullPath, Converter={StaticResource UriToBitmapConverter}}" />
That's it. Your images will now load very quickly. Tweak the thumbnail size to vary the speed versus quality.
Avi
Speeding up image loading in WPF using thumbnails的更多相关文章
- jar命令使用介绍
http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/jar.html Skip to Content Oracle Technol ...
- WPF实现炫酷Loading控件
Win8系统的Loading效果还是很不错的,网上也有人用CSS3等技术实现,研究了一下,并打算用WPF自定义一个Loading控件实现类似的效果,并可以让用户对Loading的颗粒(Particle ...
- WPF loading遮罩层 LoadingMask
原文:WPF loading遮罩层 LoadingMask 大家可能很纠结在异步query数据的时候想在wpf程序中显示一个loading的遮罩吧 今天就为大家介绍下遮罩的制作 源码下载 点击此处 先 ...
- WPF/SL: lazy loading TreeView
Posted on January 25, 2012 by Matthieu MEZIL 01/26/2012: Code update Imagine the following scenario: ...
- 【WPF】BusyIndicator做Loading遮罩层
百度了一下,粗略看了几个国内野人的做法,花了时间看下去感觉不太好用(比如有Loading居然只是作为窗体的一个局部控件的,没法全屏遮罩,那要你有何用?),于是谷歌找轮子去. 好用的轮子:http:// ...
- WPF动画 - Loading加载动画
存在问题: 最近接手公司一个比较成熟的产品项目开发(WPF桌面端),其中,在登陆系统加载时,60张图片切换,实现loading闪烁加载,快有密集恐惧症了!!! 代码如下: private void L ...
- WPF 圆形Loading
原文:WPF 圆形Loading 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/a771948524/article/details/9271933 ...
- WPF loading加载动画库
原文:WPF loading加载动画库 1. 下载Dll https://pan.baidu.com/s/1wKgv5_Q8phWo5CrXWlB9dA 2.在项目中添加引用 ...
- WPF圆形环绕的Loading动画
原文:WPF圆形环绕的Loading动画 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangyisen0713/article/details/ ...
随机推荐
- [hihoCoder] 第五十周: 欧拉路·二
题目1 : 欧拉路·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回中小Hi和小Ho控制着主角收集了分散在各个木桥上的道具,这些道具其实是一块一块骨牌. 主角 ...
- [hihoCoder] 第四十八周: 拓扑排序·二
题目1 : 拓扑排序·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho所在学校的校园网被黑客入侵并投放了病毒.这事在校内BBS上立刻引起了大家的讨论,当 ...
- 移动网络应用开发中,使用 HTTP 协议比起使用 socket 实现基于 TCP 的自定义协议有哪些优势?
HTTP 是应用层协议,TCP 是传输层协议(位于应用层之下),放在一起类比并不合适.不过猜测楼主是想对比 “标准 HTTP 协议” 还是 “自定义的协议(基于 TCP Socket)” . 一般来说 ...
- 常用的NodeJS模块
图片处理 1.Manipulate images 官网:http://github.com/aheckmann/gm ImageMagick和GraphicsMagick主要用于图片的创建.编辑.合成 ...
- 【小白的CFD之旅】19 来自计算网格的困惑
经过一年的忙碌,终于又到了寒假时间,小白又满状态复活了. 这一年小白学了很多的课程,但是一年下来,小白却感觉脑袋里没留下什么东西,貌似什么东西都在考完试的那一刹那全还回给老师了.这一年学习之余,小白仍 ...
- Java Base64 编码解码方案总结
Base64是一种能将任意Binary资料用64种字元组合成字串的方法,而这个Binary资料和字串资料彼此之间是可以互相转换的,十分方便.在实际应用上,Base64除了能将Binary资料可视化之外 ...
- Linux系统性能监控之6个vmstat和6个iostat命令
这篇文章主要介绍一些Linux性能检测相关的命令. vmstat和iostat的两个命令可以运行在主流的Linux/Unix操作系统上. 如果vmstat和iostat命令不能再你的电脑上运行,请安装 ...
- 微信小程序启动过程分析
1.微信客户端在打开小程序之前,会把整个小程序的代码包下载到本地. 2.紧接着通过 app.json 的 pages 字段就可以知道你当前小程序的所有页面路径: { "pages" ...
- DIOCP开源项目-利用队列+0MQ+多进程逻辑处理,搭建稳定,高效,分布式的服务端
最近头脑里面一直在想怎么样让能让大家基于DIOCP上写出稳定的服务端程序.很多朋友问我,你DIOCP稳定吗,我可以用他来做三层服务器吗? 当时我是这样回答的,我只能保证DIOCP底层通信的稳定. 说实 ...
- 【Professional English】Words Summary
01.数据库管理系统(Database Management Systems,DBMS) A database management system (DBMS) is a computer softw ...