Image Cache
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
namespace Photoviewer.Common
{
public static class ImageCache
{
private static readonly Dictionary<string, ImageSource> _cacheEntries;
private static readonly int _maxEntries;
static ImageCache()
{
_cacheEntries = new Dictionary<string, ImageSource>();
_maxEntries = 10;
}
/// <summary>
/// Gets a cached version of an image source
/// specified by the key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static ImageSource GetCachedImageSource(string key)
{
ImageSource source = null;
if (_cacheEntries.ContainsKey(key))
source = _cacheEntries[key];
return source;
}
/// <summary>
/// Saves an image source in the image cache
/// </summary>
/// <param name="key"></param>
/// <param name="source"></param>
public static void SaveImageSourceInCache(string key, ImageSource source)
{
if (_cacheEntries.ContainsKey(key))
{
_cacheEntries[key] = source;
return;
}
if (_cacheEntries.Count + 1 > _maxEntries)
{
_cacheEntries.Remove(_cacheEntries.Keys.First());
}
_cacheEntries.Add(key, source);
}
}
}
Related examples in the same category