title: Converting System.Drawing Bitmap to Dlib Array2D
body: <p>In this case, a grayscale Array2D for ShapePredictor.</p>
  
  <p>Here is what I am trying, without much success.</p>
  
  <pre><code>using DlibDotNet;
  using Rectangle = System.Drawing.Rectangle;
  using System.Runtime.InteropServices;
  
  public static class Extension
  {
      public static Array2D&lt;byte&gt; ToArray2D(this Bitmap bitmap)
      {
          var bits = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);
          var length = bits.Stride * bits.Height;
          var data = new byte[length];
  
          Marshal.Copy(bits.Scan0, data, 0, length);
          bitmap.UnlockBits(bits);
  
          var array = new Array2D&lt;byte&gt;(bitmap.Width, bitmap.Height);
  
          for (var x = 0; x &lt; bitmap.Width; x++)
              for (var y = 0; y &lt; bitmap.Height; y++)
              {
                  var offset = x * 4 + y * bitmap.Width * 4;
  
                  array[x][y] = data[offset];
              }
  
          return array;
      }
  </code></pre>
  
  <p>I've searched and have not yet found a clear answer.</p>