title: Color to Monochrome conversion
body: <p>See:  <em><a href="https://stackoverflow.com/a/2574798/159072">Save a 32-bit Bitmap as 1-bit .bmp file in C#</a></em></p>
  
  <p><strong>Listing #1</strong></p>
  
  <pre><code>    public static Bitmap BitmapTo1Bpp(Bitmap source)
      {
          int Width = source.Width; 
          int Height = source.Height; 
  
          Bitmap dest = new Bitmap(Width, Height, PixelFormat.Format1bppIndexed);
          BitmapData destBmpData = dest.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
  
          byte[] destBytes = new byte[(Width + 7) / 8];//19 bytes
  
          for (int y = 0; y &lt; Height; y++)
          {
              for (int x = 0; x &lt; Width; x++)
              {
                  Color c = source.GetPixel(x, y);
  
                  if (x % 8 == 0)
                  {
                      destBytes[x / 8] = 0;
                  }
                  if (c.GetBrightness() &gt;= 0.5)
                  {
                      destBytes[x / 8] |= (byte)(0x80 &gt;&gt; (x % 8));
                  }
              }
              Marshal.Copy(destBytes, 0, (IntPtr)((long)destBmpData.Scan0 + destBmpData.Stride * y), destBytes.Length);
          }
  
          dest.UnlockBits(destBmpData);
          return dest;
      }
  </code></pre>
  
  <p><strong>Listing #2</strong></p>
  
  <pre><code>    public static Bitmap BitmapTo1Bpp222(Bitmap source)
      {
          int Width = source.Width; 
          int Height = source.Height; 
  
          Bitmap dest = new Bitmap(Width, Height, PixelFormat.Format1bppIndexed);
          BitmapData destBmpData = dest.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
  
          int destStride = destBmpData.Stride;
          int destSize = Math.Abs(destStride) * Height;
  
          byte[] destBytes = new byte[destSize];
  
          for (int y = 0; y &lt; Height; y++)
          {
              for (int x = 0; x &lt; Width; x++)
              {
                  Color c = source.GetPixel(x, y);
  
                  if (x % 8 == 0)
                  {
                      destBytes[x*y / 8] = 0;
                  }
                  if (c.GetBrightness() &gt;= 0.5)
                  {
                      destBytes[x*y / 8] |= (byte)(0x80 &gt;&gt; (x % 8));
                  }
              } 
          } 
          Marshal.Copy(destBytes, 0, destBmpData.Scan0, destBytes.Length);
          dest.UnlockBits(destBmpData);
          return dest;
      } 
  </code></pre>
  
  <p>See the position of <code>Marshal.Copy()</code>.</p>
  
  <p>Why does the Listing #1 work, but Listing #2 doesn't?</p>
  
  <p>What modification can make the Listing #2 work?</p>