As RickShaub said dictionary is a good choice, you can retrieve the bitmap using the key
IDictionary<string,bitmap> imagelist=new Dictionary<string,bitmap>();
Then add your bitmaps like
imagelist.Add("Bitmap1",bitmap1); imagelist.Add("Bitmap2",bitmap2);
and retrieve like
Bitmap bitmap=imagelist["Bitmap1"];
.
All fine, but remember bitmaps
must need to disposed after use
.
An another issue if you call dispose the bitmap after added to the list the bitmap got disposed, but the reference of it still live in the list. it won't give negative if you check null and when access it you will get a fuzzy exceptions.
For example
Bitmap bmp1 = new Bitmap(100, 100);
IDictionary<string, Bitmap> list = new Dictionary<string, Bitmap>();
list.Add("bitmap1",bmp1);
bmp1.Dispose();
Bitmap bmp2 = list["bitmap1"];
if (bmp2 != null)
{
MessageBox.Show(bmp2.Width.ToString());
}
Here the bmp2 is not null, but it is not holding the unmanaged object as well.
bmp2.Width
will throw exception in this case "parameter invalid" which is really a confusing exception. Just aware of it.
If the bitmaps are too heavy for the memory, keeping all those in a list would be undesirable.
I would recommend keeping those bitmap data in an array and convert to bitmap, use it, and dispose whenever necessary (if this is not going to be an iterative cycle). This is not a performance optimization, but to avoid memory leaks.
Just my view. Listen to other experts as well.