private bool InitializeBitmap() { if (!_forceLayout && CellBitmap != null && CellBitmap.Width == Width && CellBitmap.Height == Height) { return(false); } if (CellBitmap != null) { mem -= CellBitmap.Width * CellBitmap.Height * 4; CellBitmap.Dispose(); } //Peg to 10MB const int megs = 10; if (mem + (Width * Height * 4) > megs * 1024 * 1024) { // decimate memory usage while (mem + (Width * Height * 4) > 1024 * 1024) { var bit = Bitmaps.FirstOrDefault(); if (bit.Value == null) { break; } mem -= bit.Value.Width * bit.Value.Height * 4; Bitmaps.Remove(bit.Key); bit.Value.Dispose(); } } try { Bitmaps[this] = new Bitmap(Width, Height); mem += Width * Height * 4; } catch (OutOfMemoryException) { return(false); } return(true); }