异步缩略图转换器
本文关键字:转换器 略图 异步 | 更新日期: 2023-09-27 18:07:24
我在WPF的缩略图创建中遇到了一个问题。虽然我可以在不同的线程中创建缩略图和图像,但我不能将其转移到主UI线程。
转换成缩略图已经处理,所以这不再是一个问题;我现在就是这样做的:
public ImageSource Image
{
get
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.DecodePixelWidth = 100;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = new Uri(value.ToString());
bi.EndInit();
return bi;
}
}
<Image Source="{Binding Image, IsAsync=True}"/>
,
我如何设法将缩略图传递给主线程?
提前感谢!
为了使BitmapImage跨线程可访问,您必须冻结它。你还应该保留已创建的BitmapImage,以防getter被多次调用。
private BitmapImage image;
public ImageSource Image
{
get
{
if (image == null)
{
image = new BitmapImage();
image.BeginInit();
image.DecodePixelWidth = 100;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(value.ToString());
image.EndInit();
image.Freeze(); // here
}
return image;
}
}