title: Use native HBitmap in C# while preserving alpha channel/transparency
body: <p>Let's say I get a HBITMAP object/handle from a native Windows function. I can convert it to a managed bitmap using <em>Bitmap.FromHbitmap(nativeHBitmap)</em>, but if the native image has transparency information (alpha channel), it is lost by this conversion.</p>
  
  <p>There are a few questions on Stack Overflow regarding this issue. Using information from the first answer of this question (<a href="https://stackoverflow.com/questions/182988/how-to-draw-argb-bitmap-using-gdi">How to draw ARGB bitmap using GDI+?</a>), I wrote a piece of code that I've tried and it works.</p>
  
  <p>It basically gets the native HBitmap width, height and the pointer to the location of the pixel data using <em>GetObject</em> and the <em>BITMAP</em> structure, and then calls the managed Bitmap constructor:</p>
  
  <pre><code>Bitmap managedBitmap = new Bitmap(bitmapStruct.bmWidth, bitmapStruct.bmHeight,
      bitmapStruct.bmWidth * 4, PixelFormat.Format32bppArgb, bitmapStruct.bmBits);
  </code></pre>
  
  <p>As I understand (please correct me if I'm wrong), this does not copy the actual pixel data from the native HBitmap to the managed bitmap, it simply points the managed bitmap to the pixel data from the native HBitmap.</p>
  
  <p>And I don't draw the bitmap here on another Graphics (DC) or on another bitmap, to avoid unnecessary memory copying, especially for large bitmaps.</p>
  
  <p>I can simply assign this bitmap to a PictureBox control or the the Form BackgroundImage property. And it works, the bitmap is displayed correctly, using transparency.</p>
  
  <p>When I no longer use the bitmap, I make sure the BackgroundImage property is no longer pointing to the bitmap, and I dispose both the managed bitmap and the native HBitmap.</p>
  
  <p><strong>The Question:</strong> Can you tell me if this reasoning and code seems correct. I hope I will not get some unexpected behaviors or errors. And I hope I'm freeing all the memory and objects correctly.</p>
  
  <pre><code>    private void Example()
      {
          IntPtr nativeHBitmap = IntPtr.Zero;
  
          /* Get the native HBitmap object from a Windows function here */
  
          // Create the BITMAP structure and get info from our nativeHBitmap
          NativeMethods.BITMAP bitmapStruct = new NativeMethods.BITMAP();
          NativeMethods.GetObjectBitmap(nativeHBitmap, Marshal.SizeOf(bitmapStruct), ref bitmapStruct);
  
          // Create the managed bitmap using the pointer to the pixel data of the native HBitmap
          Bitmap managedBitmap = new Bitmap(
              bitmapStruct.bmWidth, bitmapStruct.bmHeight, bitmapStruct.bmWidth * 4, PixelFormat.Format32bppArgb, bitmapStruct.bmBits);
  
          // Show the bitmap
          this.BackgroundImage = managedBitmap;
  
          /* Run the program, use the image */
          MessageBox.Show("running...");
  
          // When the image is no longer needed, dispose both the managed Bitmap object and the native HBitmap
          this.BackgroundImage = null;
          managedBitmap.Dispose();
          NativeMethods.DeleteObject(nativeHBitmap);
      }
  
  internal static class NativeMethods
  {
      [StructLayout(LayoutKind.Sequential)]
      public struct BITMAP
      {
          public int bmType;
          public int bmWidth;
          public int bmHeight;
          public int bmWidthBytes;
          public ushort bmPlanes;
          public ushort bmBitsPixel;
          public IntPtr bmBits;
      }
  
      [DllImport("gdi32", CharSet = CharSet.Auto, EntryPoint = "GetObject")]
      public static extern int GetObjectBitmap(IntPtr hObject, int nCount, ref BITMAP lpObject);
  
      [DllImport("gdi32.dll")]
      internal static extern bool DeleteObject(IntPtr hObject);
  }
  </code></pre>