title: Caching bitmaps when working with pointers
body: <p>I have two function: one of them for getting image from .mul file like a byte array into bitmap, the second going to paint it in colors (change pixels of it image). So, here is the function of painting bitmap in colors, and this function caching bitmap:</p>
  
  <pre><code>    public static void RecolorPartial(HueEntry hue, Bitmap bitmap)
      {
          if (hue == null)
              throw new ArgumentNullException("hue");
  
          if (bitmap == null)
              return;
  
          if (bitmap.PixelFormat != PixelFormat.Format16bppArgb1555)
              throw new ArgumentException("Invalid bitmap pixel format.", "source");
  
          BitmapData data = bitmap.LockBits(Ultima.GetBitmapBounds(bitmap), ImageLockMode.ReadWrite, PixelFormat.Format16bppArgb1555);
          Debug.Assert(data.Stride % 2 == 0);
  
          RecolorPartialInternal(hue, data.Scan0, data.Stride * data.Height / 2);
  
          bitmap.UnlockBits(data);
      }
  
      private static unsafe void RecolorPartialInternal(HueEntry hue, IntPtr dataPtr, int lenght)
      {
          ushort* pData = (ushort*)dataPtr.ToPointer();
  
          for (int i = 0; i &lt; lenght; i++)
          {
              ushort c = (ushort)((*pData) &amp; 0x1F);
              if (c == (((*pData) &gt;&gt; 5) &amp; 0x1F) &amp;&amp; c == (((*pData) &gt;&gt; 10) &amp; 0x1F))
              {
                  *pData = (ushort)(((*pData) &amp; 0x8000) | hue.Colors[c]);
              }
              pData++;
          }
      }
  </code></pre>
  
  <p>I can't understand, why when i use it in cycle (for 5-10 bitmaps (but 2 already enought)), it's going to cache it or didn't redraw- after first bitmap, every next looks like first.</p>
  
  <pre><code>        for (int i = 0; i &lt; colors.Count(); i++)
          {
              Bitmap bit = Art.GetStatic(type);
  
              Hues hues = Hues.Load(huePath);
  
              if (colors[i] != 0)
                  RedrawHue.RecolorPartial(hues[colors[i]], bit);
  
              Bitmap newbit = new Bitmap(bit.Width, bit.Height);
  
              Graphics newgraph = Graphics.FromImage(newbit);
  </code></pre>
  
  <p>...</p>
  
  <p>If my colors array looks like {0, 1590, 0, 1330, 0, 1777} - that's fine, images changing from gray (for 0 color value) to blue (1590 value) , but 1590 = 1330 = 1777. If i make {1590, 1330, 1777, 2000} - all of bitmaps the same and looks like for 1590 (ofcource if i shuffle- every next after first should be like first). Without cycle everything fine.
  Mb the question is stupid, isn't full or isn't described well, but i really don't know why is that and what i can to do for using this function in cycle.</p>