WPF缓存图像数据的最佳方法是什么

时间:2013-08-25 13:24:57

标签: wpf image byte bitmapimage

我有一个应用程序,我以byte []的形式获得了大量图像, 我将它们存储在内存中供以后用户需求使用

我应该将它们存储在byte []中吗?或者还有另一种方法来存储它们以便更快地加载用户需求?

我加载图片的代码就像这样

    private static BitmapImage LoadImage(byte[] imageData)
    {
        if (imageData == null || imageData.Length == 0) return null;
        var image = new BitmapImage();
        using (var mem = new MemoryStream(imageData))
        {
            image.BeginInit();
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.StreamSource = mem;
            image.EndInit();
        }
        image.Freeze();
        return image;
    }

谢谢! 罗恩

1 个答案:

答案 0 :(得分:2)

可以将图像存储在字典中 关键是唯一标识符(E.G.Int32)。

图像可以存储为byte []或BitmapImage

如果将其存储为BitmapImage,则必须在前面转换byte [] 但是,您不需要按需转换

Dictionary<Int32, byte[]>  
or
Dictionary<Int32, BitmapImage> 

非常确定BitmapImage会更大,因此按需转换会占用更少的内存 你的问题说了很多图片,但你也要求更快的用户加载 测试两种方式。

相关问题