title: C# Image.Clone Out of Memory Exception
body: <p>Why am I getting an out of memory exception?</p>
  
  <p>So this dies in C# on the first time through:</p>
  
  <p><strong>splitBitmaps.Add(neededImage.Clone(rectDimensions, neededImage.PixelFormat));</strong> </p>
  
  <p>Where splitBitmaps is a List&lt;BitMap&gt; BUT this works in VB for at least 4 iterations:</p>
  
  <p><strong>arlSplitBitmaps.Add(Image.Clone(rectDimensions, Image.PixelFormat))</strong></p>
  
  <p>Where arlSplitBitmaps is a simple array list.  (And yes I've tried arraylist in c#)</p>
  
  <p>This is the fullsection:</p>
  
  <pre><code>for (Int32 splitIndex = 0; splitIndex &lt;= numberOfResultingImages - 1; splitIndex++)
  { 
    Rectangle rectDimensions;
  
    if (splitIndex &lt; numberOfResultingImages - 1) 
    {
      rectDimensions = new Rectangle(splitImageWidth * splitIndex, 0, 
        splitImageWidth, splitImageHeight); 
    } 
    else 
    {
      rectDimensions = new Rectangle(splitImageWidth * splitIndex, 0, 
       sourceImageWidth - (splitImageWidth * splitIndex), splitImageHeight); 
    } 
  
    splitBitmaps.Add(neededImage.Clone(rectDimensions, neededImage.PixelFormat)); 
  </code></pre>
  
  <p>} </p>
  
  <p>neededImage is a Bitmap by the way. </p>
  
  <p>I can't find any useful answers on the intarweb, especially not why it works just fine in VB.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I actually found a reason (sort of) for this working but forgot to post it.  It has to do with converting the image to a bitmap instead of just trying to clone the raw image if I remember.</p>