たくさんの画像をダウンロードして、データバインディングを介してリストボックス内に表示しています。すなわち
...
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding ImageUrl}" Height="90" Width="90" Stretch="UniformToFill" />
</DataTemplate>
</ListBox.ItemTemplate>
...
画像のサムネイルが欲しいのですが。イメージコントロールを90x90に設定しても、イメージは元のフルサイズでデコードされるため、必要以上に多くのメモリを消費します。
この目的で使用できるPictureDecoderクラスがありますが、その外観からすると、バックグラウンドスレッドでは使用できません。
ThreadPoolとWriteableBitmapを使用する添付の依存関係プロパティを作成してみました。
public static readonly DependencyProperty DecodingSourceProperty = DependencyProperty.RegisterAttached(
DecodingSourcePropertyName,
typeof (Uri),
typeof (Image),
new PropertyMetadata(null, OnDecodingSourcePropertyChanged));
static void OnDecodingSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var img = d as Image;
double height = img.Height;
double width = img.Width;
var uri = (Uri)e.NewValue;
var bmp = new WriteableBitmap((int)width, (int)height);
ThreadPool.QueueUserWorkItem(callback => {
var web = new WebClient();
web.OpenReadCompleted += (sender, evt) => {
bmp.LoadJpeg(evt.Result);
evt.Result.Dispose();
Deployment.Current.Dispatcher.
BeginInvoke(() = > {
img.Source = bmp;
});
};
web.OpenReadAsync(uri);
}
});
}
<Image helpers:ImageExt.DecodingSource="{Binding ImageUrl}" Height="90" Width="90" Stretch="UniformToFill" />
しかし、それは私が設定したストレッチ特性を尊重していません。
同様の目的を果たすことができるサードパーティのコントロールはありますか?
サーバー上の画像のサイズを変更する必要はありませんが、最も簡単な方法のようです。