title: Bitmap Region is already locked exception
body: <p>I am using this code:</p>
  
  <pre><code>internal static Image ColorReplacer(Image Img, Color oldcolor, Color newcolor, int tolerence)
          {
              // Gotten From -&gt; Code Project
              Bitmap bmap = (Bitmap)Img.Clone();
              Color c;
              int iR_Min, iR_Max; int iG_Min, iG_Max; int iB_Min, iB_Max;
              //Defining Tolerance
              //R
              iR_Min = Math.Max((int)oldcolor.R - tolerence, 0);
              iR_Max = Math.Min((int)oldcolor.R + tolerence, 255);
              //G
              iG_Min = Math.Max((int)oldcolor.G - tolerence, 0);
              iG_Max = Math.Min((int)oldcolor.G + tolerence, 255);
              //B
              iB_Min = Math.Max((int)oldcolor.B - tolerence, 0);
              iB_Max = Math.Min((int)oldcolor.B + tolerence, 255);
  
              for (int x = 0; x &lt; bmap.Width; x++)
              {
                  for (int y = 0; y &lt; bmap.Height; y++)
                  {
                      c = bmap.GetPixel(x, y);
                      //Determinig Color Match
                      if ((c.R &gt;= iR_Min &amp;&amp; c.R &lt;= iR_Max) &amp;&amp;
                          (c.G &gt;= iG_Min &amp;&amp; c.G &lt;= iG_Max) &amp;&amp;
                          (c.B &gt;= iB_Min &amp;&amp; c.B &lt;= iB_Max)
                         )
                          if (newcolor == Color.Transparent)
                              bmap.SetPixel(x, y, Color.FromArgb(0, newcolor.R, newcolor.G, newcolor.B));
                          else
                              bmap.SetPixel(x, y, Color.FromArgb(c.A, newcolor.R, newcolor.G, newcolor.B));
                  }
              }
              return (Image)bmap.Clone();
          }
  </code></pre>
  
  <p>This code works very well. It changes my white icon image to another color successfully.
  The problem is: Once I change it, I cannot change it again. It gives me the "Bitmap Region is already locked exception". I am assuming it's because GetPixel() is locking the image?</p>
  
  <p>can anybody suggest a good solution to this problem ?</p>
  
  <p>PS: I understand that GetPixel() is a very slow method, however, I am using 8 image and they are all 24px. They are very small so I don't think GetPixel()'s performance is that big of a problem.</p>