Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Managing List<Bitmap>

Managing List<Bitmap>

Scheduled Pinned Locked Moved C#
questiongraphicsperformancelounge
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BBatts
    wrote on last edited by
    #1

    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

    OriginalGriffO P 2 Replies Last reply
    0
    • B BBatts

      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

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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)

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      B 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        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)

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #3

        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

        OriginalGriffO 1 Reply Last reply
        0
        • B BillWoodruff

          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

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          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)

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • B BBatts

            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

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups