Managing List<Bitmap>
-
Hello All, I have an application that frequently uses the general List object with an bitmap type". I have two questions concerning these. 1: What is the best way to copy the bitmaps from one list to another while ensuring the Bitmaps in the original list are unchanged if changes are made to the new list. Currently i'm doing it in a forloop such as:
List aviList1 = new List(); aviList1.Clear();
for (int i=0; I originalList.Count; i++)
aviList1.Add(new Bitmap(originalList[i]));2: Is calling originalList.Clear sufficient to free up the memory or would it be necessary to loop through the list and dispose of the Bitmaps individually? Thanks in advance for the always great support here at CodeProject. Bryan
-
Hello All, I have an application that frequently uses the general List object with an bitmap type". I have two questions concerning these. 1: What is the best way to copy the bitmaps from one list to another while ensuring the Bitmaps in the original list are unchanged if changes are made to the new list. Currently i'm doing it in a forloop such as:
List aviList1 = new List(); aviList1.Clear();
for (int i=0; I originalList.Count; i++)
aviList1.Add(new Bitmap(originalList[i]));2: Is calling originalList.Clear sufficient to free up the memory or would it be necessary to loop through the list and dispose of the Bitmaps individually? Thanks in advance for the always great support here at CodeProject. Bryan
Bitmaps are a scarce resource: they implement IDisposable, so you should clean up behind you, and Dispose all bitmaps you create. If you don't you may well get "Out of Memory" problems long before the actual RAM is exhausted. Additionally, depending on how you initially constructed the bitmaps, you may leave files or streams in use until the bitmap.Dispose method is called by your code, or by the garbage collector. You need to construct a new image for each that you add to the other list, if you want the original unmodified when the aviList1[index] bitmap is changed. However, it would look tidier with a foreach loop:
foreach (Bitmap b in originalList)
aviList1.Add(new Bitmap(b));Or you could do it with Linq:
aviList.AddRange(originalList.Select(bm => new Bitmap(bm)));
(But that just hides the loop)
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
Bitmaps are a scarce resource: they implement IDisposable, so you should clean up behind you, and Dispose all bitmaps you create. If you don't you may well get "Out of Memory" problems long before the actual RAM is exhausted. Additionally, depending on how you initially constructed the bitmaps, you may leave files or streams in use until the bitmap.Dispose method is called by your code, or by the garbage collector. You need to construct a new image for each that you add to the other list, if you want the original unmodified when the aviList1[index] bitmap is changed. However, it would look tidier with a foreach loop:
foreach (Bitmap b in originalList)
aviList1.Add(new Bitmap(b));Or you could do it with Linq:
aviList.AddRange(originalList.Select(bm => new Bitmap(bm)));
(But that just hides the loop)
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
Hi OG, I am inferring from your answer (upvoted), that, in this case, it would be best for the OP to loop through his List and specifically dispose of them: that he should not rely on just clearing the List: is that correct ? thanks, Bill
~ “This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal
-
Hi OG, I am inferring from your answer (upvoted), that, in this case, it would be best for the OP to loop through his List and specifically dispose of them: that he should not rely on just clearing the List: is that correct ? thanks, Bill
~ “This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal
Yes - clearing the list does not dispose of any of the items on it (thank goodness!) so the bitmaps will be left hanging until the garbage collector gets round to disposing of them for you.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
Hello All, I have an application that frequently uses the general List object with an bitmap type". I have two questions concerning these. 1: What is the best way to copy the bitmaps from one list to another while ensuring the Bitmaps in the original list are unchanged if changes are made to the new list. Currently i'm doing it in a forloop such as:
List aviList1 = new List(); aviList1.Clear();
for (int i=0; I originalList.Count; i++)
aviList1.Add(new Bitmap(originalList[i]));2: Is calling originalList.Clear sufficient to free up the memory or would it be necessary to loop through the list and dispose of the Bitmaps individually? Thanks in advance for the always great support here at CodeProject. Bryan
If I may be so bold as to humbly point you to a little article[^] that I wrote a while back on memory managing images, you might find that it helps a lot. I use this a lot.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier