我對wpf很陌生,遇到以下問題。圖像組件上的內存泄漏
我有一個列表框,裏面充滿了指向系統中存儲文件的路徑。 我使用下面的代碼來完成這個
spriteImg.Source = new BitmapImage(new Uri(images));
問題是,直到它需要大約一秒鐘的圖像更新時,快速瀏覽lisbox圖像更新變得緩慢。
任何建議如何我可以克服這個問題將不勝感激。
問候
我對wpf很陌生,遇到以下問題。圖像組件上的內存泄漏
我有一個列表框,裏面充滿了指向系統中存儲文件的路徑。 我使用下面的代碼來完成這個
spriteImg.Source = new BitmapImage(new Uri(images));
問題是,直到它需要大約一秒鐘的圖像更新時,快速瀏覽lisbox圖像更新變得緩慢。
任何建議如何我可以克服這個問題將不勝感激。
問候
// Create the image element.
Image simpleImage = new Image();
simpleImage.Width = 200;
simpleImage.Margin = new Thickness(5);
// Create source.
BitmapImage bi = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit();
bi.UriSource = new Uri(@"/Images/1.jpg",UriKind.RelativeOrAbsolute);
bi.EndInit();
// Set the image source.
simpleImage.Source = bi;
這ASLO可以幫助你:
你可以考慮BitmapCacheOption.None。圖像將直接從磁盤讀取,而不是緩存在內存中。
實施例如何從存儲器流使用:
using (MemoryStream stream = new MemoryStream()) {
bitmap.Save(stream, ImageFormat.Bmp);
stream.Position = 0;
BitmapImage result = new BitmapImage();
result.BeginInit();
// According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
// Force the bitmap to load right now so we can dispose the stream.
result.CacheOption = BitmapCacheOption.OnLoad;
result.StreamSource = stream;
result.EndInit();
result.Freeze();
}
我使用該圖像的存儲器流。和滾動問題是由添加以下行
VirtualizingPanel.VirtualizationMode="Recycling"
感謝您指出我在正確的方向@Jehof
你保持圖像的存儲解決? – Sasha
ListBox使用VirtualizingStackPanel,所以它可能是當您滾動它需要一些時間來顯示圖像 – Jehof