2013-10-08 39 views
0

我对wpf很陌生,遇到以下问题。图像组件上的内存泄漏

我有一个列表框,里面充满了指向系统中存储文件的路径。 我使用下面的代码来完成这个

spriteImg.Source = new BitmapImage(new Uri(images)); 

问题是,直到它需要大约一秒钟的图像更新时,快速浏览lisbox图像更新变得缓慢。

任何建议如何我可以克服这个问题将不胜感激。

问候

+0

你保持图像的存储解决? – Sasha

+0

ListBox使用VirtualizingStackPanel,所以它可能是当您滚动它需要一些时间来显示图像 – Jehof

回答

0
// 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(); 

    } 
0

我使用该图像的存储器流。和滚动问题是由添加以下行

VirtualizingPanel.VirtualizationMode="Recycling" 

感谢您指出我在正确的方向@Jehof