我正在尝试使用WPF编写一个小型照片查看器,基本上是模拟 Windows 照片查看器 的功能。
使用 Image
来在窗口模式和全屏模式下显示图片。
<Image Name="ImgDisplay" Source="{Binding CurrentImage.FullPath, Converter={StaticResource FilenameToImageConverter}}"/>
FilenameToImageConverter
执行以下操作:
public class FilenameToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string uri = value as string;
if (uri != null && File.Exists(uri))
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.None;
image.UriCachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = new Uri(uri);
image.EndInit();
return image;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
当我使用我的照片(大约8兆像素,4MB的jpeg文件)测试程序时,显示图像的加载时间非常长(2或3秒),而Windows照片查看器能够轻松跳过图像。我首先看到它显示图像的低分辨率版本,不久之后才会显示完整的图像。然而,一切都比我的方法快得多。
如何实现这一点?是通过缩略图/预加载吗? 提前感谢您的回答。
编辑
感谢所给的提示,使用
DecodePixelWidth
进行缩小以及Async / OneWay-Bindings已经显著改善了情况,但还不足以使所有内容流畅。此外,使用IsAsync=true
,在加载下一张图片之前,图像将始终为空白,这是一种不愉快的效果。希望通过立即显示高度缩小的版本,然后在异步加载完整图像后显示完整图像来解决这个问题。由于涉及某种时间上的连续性,我不知道如何使用绑定来实现。请问是否有任何建议?