public static BitmapSource RenderBitmap(string text, double font_size, Brush brush) { // If the cache is too large, remove the first element repeatedly until it’s OK. while (m_cache.Count >= MaxCachedBitmaps || m_cached_pixels >= MaxCachedPixels) { var it = m_cache.GetEnumerator(); it.MoveNext(); m_cached_pixels -= ((RenderKey)it.Key).Pixels; m_cache.RemoveAt(0); } // Query the cache for this bitmap and render it if not found BitmapSource ret; var key = new RenderKey() { Text = text, FontSize = font_size, Foreground = brush }; if (m_cache.Contains(key)) { ret = m_cache[key] as BitmapSource; m_cache.Remove(key); m_cache[key] = ret; ++m_cache_hits; } else { m_cache[key] = ret = RenderBitmapInternal(text, font_size, brush); m_cached_pixels += key.Pixels; ++m_cache_misses; } return(ret); }
public static BitmapSource RenderBitmap(string text, double font_size) { var key = new RenderKey() { Text = text, FontSize = font_size }; if (!m_cache.TryGetValue(key, out var ret)) { m_cache[key] = ret = RenderBitmapInternal(text, font_size); } return(ret); }