I am wondering how it allocates and deletes the memory allocated for bitmap images in .NET.
When I make a lot of raster creations in loops in a function and call them sequentially, it will work until at some point the bitmap can allocate memory, giving an exception "Invalid parameter" for the specified size.
If I call the garbage collector while it works.
With the following code, you can recompose the error:
class BitmapObject { public bool Visible { get { return enb; } set { enb = value; } } private bool enb; private Bitmap bmp; public BitmapObject(int i, bool en) { enb = en; bmp = new Bitmap(i, i); } } class Pool<T> where T : BitmapObject { List<T> preallocatedBitmaps = new List<T>(); public void Fill() { Random r = new Random(); for (int i = 0; i < 500; i++) { BitmapObject item = new BitmapObject(500, r.NextDouble() > 0.5); preallocatedBitmaps.Add(item as T); } } public IEnumerable<T> Objects { get { foreach (T component in this.preallocatedBitmaps) { if (component.Visible) { yield return (T)component; } } } } } static class Program {
Marino Šimić
source share