title: C#: Simulate gdi GetBitmapBits function and GdiFlush to fill a byte array with a bitmap
body: <p>I created a lib where I need to fill a byte array of 307200 elements (320x240x4(=32bit)) in <strong>the correct order</strong> for a bitmap, the display uses the format RGBA, and I would like to avoid using interop to use GetBitmapBits as I'm doing at the moment, while I prefer writing it in c# code to understand how bytes are printed on it.</p>
  
  <p>Can someone help me?</p>
  
  <p>Here is my actual code</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// LONG GetBitmapBits(
      ///    __in   HBITMAP hbmp,
      ///    __in   LONG cbBuffer,
      ///    __out  LPVOID lpvBits
      ///  );
      /// &lt;/summary&gt;
      /// &lt;param name="hbmp"&gt;&lt;/param&gt;
      /// &lt;param name="cbBuffer"&gt;&lt;/param&gt;
      /// &lt;param name="lpvBits"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      [DllImport("Gdi32", EntryPoint = "GetBitmapBits")]
      private extern static long GetBitmapBits([In] IntPtr hbmp, [In] int cbBuffer, [Out] byte[] lpvBits);
  
      [DllImport("Gdi32", EntryPoint = "GdiFlush")]
      private extern static void GdiFlush();
  
      private void FillPixelArray(Bitmap bmp, ref byte[] array, bool bw = false)
      {
          Color tmp;
          if (!bw)
          {
              IntPtr hbmp = bmp.GetHbitmap();
              GdiFlush();
              GetBitmapBits(hbmp, array.Length * Marshal.SizeOf(typeof(byte)), array);
          }
          else
          {
              for (int x = 0; x &lt; LgLcd.NativeConstants.LGLCD_BMP_WIDTH; ++x)
              {
                  for (int y = 0; y &lt; LgLcd.NativeConstants.LGLCD_BMP_HEIGHT; ++y)
                  {
                      tmp = bmp.GetPixel(x, y);
                      array[y * 160 + x] = (byte)((tmp.R == 255 &amp;&amp; tmp.G == 255 &amp;&amp; tmp.B == 255) ? 0 : 255);
                  }
              }
          }
      }
  </code></pre>
  
  <p>Another thing, is GetBitmapBits faster than any implementation I can do in C#?</p>