title: How are Bitmaps stored in memory in .NET?
body: <p>In .NET you usually use the Bitmap class to store images. To access it quickly, you need to call <a href="http://www.codeproject.com/Tips/240428/Work-with-bitmap-faster-with-Csharp" rel="nofollow">lock() and unlock()</a> to copy the contents of the bitmap into memory. So, does it mean that the bitmap was not stored in a packed memory array before locking?</p>
  
  <p>Exactly what is the need for locking?, i.e. why couldn't the platform simply return a pointer to the first pixel of the bitmap and let you access the pixels directly? (apart from the "unsafe" memory access considerations)</p>
  
  <p>Some possible reasons:</p>
  
  <ul>
  <li>The bitmap is stored in its original compressed form to save memory (PNG, JPEG, etc)</li>
  <li>The bitmap is stored in 24-bpp format so its slower to access than a 32-bpp image</li>
  <li>The bitmap is not stored in a packed memory array and is fragmented so can't be read/written fast</li>
  <li>The bitmap is stored in an undisclosed manner, and the platform does not want you to access the actual bitmap data memory - forcing you to create a copy of it in memory using lock()</li>
  </ul>