I have an application where I get lot of images in the form of byte[], I store them in the memory for later use by the user demand

Should I store them in byte[]? or there is another way to store them for quicker loading on user demand?

My code that loads the image is like this

    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;
    }

Thank you! Ron

有帮助吗?

解决方案

Could store the images in a Dictionary.
The key is the unique identifier (E.G. Int32).

The image could be stored as byte[] or BitmapImage

If you store it as BitmapImage you have to convert the byte[] up front
But then you don't need to convert on demand

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

Pretty sure BitmapImage is going to be bigger so converting on demand would use less memory.
Your question said a lot of images but you also asked for quicker user loading.
Test both ways.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top