title: Purpose of ImageLockMode in Bitmap.LockBits (with code)
body: <p>What is the purpose of ImageLockMode in Bitmap.LockBits?
  For ReadOnly the <a href="https://msdn.microsoft.com/en-us/library/system.drawing.imaging.imagelockmode(v=vs.110).aspx" rel="nofollow noreferrer">documentation</a> only states that</p>
  
  <blockquote>
    <p>ReadOnly: Specifies that a portion of the image is locked for reading.</p>
  </blockquote>
  
  <p>But the following code proves, that this is not true.
  I know the question has been asked before, this time I try with some actual code as I couldn't find an answer anywhere else.</p>
  
  <p>If I run the following code, it behaves exactly as explained in the answer.</p>
  
  <pre><code>using System;
  using System.Drawing;
  using System.Drawing.Imaging;
  using System.Runtime.InteropServices;
  
  namespace LockBits_Trials
  {
     class Program
     {
        static readonly Random rnd = new Random(42);
        static void Main(string[] args)
        {
           Bitmap bmp_fromFile = new Bitmap("example.png");
           Bitmap bmp_fromCtor = new Bitmap(100, 100, PixelFormat.Format24bppRgb);
           marshalCopy(bmp_fromFile, "result_marshalCopy_fromFile.png");
           marshalCopy(bmp_fromCtor, "result_marshalCopy_fromCtor.png");
           usePointer(bmp_fromFile, "result_usePointer_fromFile.png");
           usePointer(bmp_fromCtor, "result_usePointer_fromCtor.png");
        }
  
        private static unsafe void usePointer(Bitmap bmp, string filename)
        {
           ImageLockMode mode = ImageLockMode.ReadOnly;
           //code from turgay at http://csharpexamples.com/fast-image-processing-c/ 
           if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
              throw new Exception();
           BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), mode, bmp.PixelFormat);
           int bytesPerPixel = 3; int heightInPixels = bitmapData.Height; int widthInBytes = bitmapData.Width * bytesPerPixel;
           byte* ptrFirstPixel = (byte*)bitmapData.Scan0;
           for (int y = 0; y &lt; heightInPixels; y++) {
              byte* currentLine = ptrFirstPixel + (y * bitmapData.Stride);
              for (int x = 0; x &lt; widthInBytes; x = x + bytesPerPixel) {
                 currentLine[x] = (byte)rnd.Next(0, 255);
                 currentLine[x + 1] = (byte)rnd.Next(0, 255);
                 currentLine[x + 2] = (byte)rnd.Next(0, 255);
              }
           }
           bmp.UnlockBits(bitmapData);
           bmp.Save(filename, ImageFormat.Png);
        }
  
        private static unsafe void marshalCopy(Bitmap bmp, string filename)
        {
           ImageLockMode mode = ImageLockMode.ReadOnly;
           if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
              throw new Exception();
           BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), mode, bmp.PixelFormat);
           IntPtr ptrFirstPixel = bitmapData.Scan0;
           int totalBytes = bitmapData.Stride * bitmapData.Height;
           byte[] newData = new byte[totalBytes];
           for (int i = 0; i &lt; totalBytes; i++)
              newData[i] = (byte)rnd.Next(0, 255);
           Marshal.Copy(newData, 0, ptrFirstPixel, newData.Length);
           bmp.UnlockBits(bitmapData);
           bmp.Save(filename, ImageFormat.Png);
        }
     }
  }
  </code></pre>
  
  <p>The pictures <strong>result_marshalCopy_fromFile.png</strong> and <strong>result_usePointer_fromFile.png</strong> both contain the original image, so nothing was overwritten (and no Exception thrown!).
  The other two pictures contain the random noise, that was written to them while being locked.</p>
  
  <p>I did not do further test to confirm the behaviour of parallel write access, as I don't do that anyways.</p>
  
  <p>It's not a duplicate, but strongly related:
  <a href="https://stackoverflow.com/questions/15183958">Does Bitmap.LockBits “pin” a bitmap into memory?</a></p>