我正在下載一堆圖像,並通過數據綁定在列表框中顯示它們。 即圖像解碼(縮略圖)和使用數據綁定
...
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding ImageUrl}" Height="90" Width="90" Stretch="UniformToFill" />
</DataTemplate>
</ListBox.ItemTemplate>
...
我想要縮略圖的圖像。儘管將圖像控制設置爲90x90,但圖像仍會以其原始大小進行解碼,因此佔用的內存比應該多。
有一個PictureDecoder類可以用於此目的,但通過它的外觀,它不能用於後臺線程。
我試圖創建一種使用線程池和一個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" />
,但它不尊重延伸性我設置。
我想知道是否有任何第三方控件可以用於類似目的?
我想避免在服務器上調整圖像大小 - 儘管它似乎是最簡單的方法。
沒有真正回答您的問題,但如果您調整客戶端的圖片大小,您將首先在內存中加載全尺寸版本。所以在服務器端處理調整大小更容易,並且減少了內存佔用。 –
有效的一點。我想我可能會以服務器結束:/。想要避免它,因爲我需要維護一個。不過,也許有希望與一些孤立的存儲緩存的轉換圖像等 – zyash