0

我对 wpf 很陌生,遇到以下问题。

我有一个列表框,其中充满了指向存储在系统上的文件的路径。我正在使用以下代码来完成此操作

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

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

任何关于我如何克服这个问题的建议将不胜感激。

问候

4

2 回答 2

0

我正在为图像使用内存流。通过添加以下行解决了滚动问题

    VirtualizingPanel.VirtualizationMode="Recycling"

感谢您为我指明正确的方向@Jehof

于 2013-10-10T13:12:45.413 回答
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;

这也可以帮助您:

您可能会考虑 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();

    }
于 2013-10-09T08:17:35.063 回答