private bool bFillBitmapInfo(IntPtr hdc, IntPtr hpal, ref System.Drawing.NativeMethods.BITMAPINFO_FLAT pbmi)
        {
            IntPtr zero = IntPtr.Zero;
            bool   flag = false;

            try
            {
                zero = SafeNativeMethods.CreateCompatibleBitmap(new HandleRef(null, hdc), 1, 1);
                if (zero == IntPtr.Zero)
                {
                    throw new OutOfMemoryException(System.Drawing.SR.GetString("GraphicsBufferQueryFail"));
                }
                pbmi.bmiHeader_biSize = Marshal.SizeOf(typeof(System.Drawing.NativeMethods.BITMAPINFOHEADER));
                pbmi.bmiColors        = new byte[0x400];
                SafeNativeMethods.GetDIBits(new HandleRef(null, hdc), new HandleRef(null, zero), 0, 0, IntPtr.Zero, ref pbmi, 0);
                if (pbmi.bmiHeader_biBitCount <= 8)
                {
                    return(this.bFillColorTable(hdc, hpal, ref pbmi));
                }
                if (pbmi.bmiHeader_biCompression == 3)
                {
                    SafeNativeMethods.GetDIBits(new HandleRef(null, hdc), new HandleRef(null, zero), 0, pbmi.bmiHeader_biHeight, IntPtr.Zero, ref pbmi, 0);
                }
                flag = true;
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    SafeNativeMethods.DeleteObject(new HandleRef(null, zero));
                    zero = IntPtr.Zero;
                }
            }
            return(flag);
        }
        /// <summary>
        /// Fills in the fields of a BITMAPINFO so that we can create a bitmap
        /// that matches the format of the display.
        ///
        /// This is done by creating a compatible bitmap and calling GetDIBits
        /// to return the color masks. This is done with two calls. The first
        /// call passes in biBitCount = 0 to GetDIBits which will fill in the
        /// base BITMAPINFOHEADER data. The second call to GetDIBits (passing
        /// in the BITMAPINFO filled in by the first call) will return the color
        /// table or bitmasks, as appropriate.
        /// </summary>
        /// <returns>True if successful, false otherwise.</returns>
        private bool FillBitmapInfo(IntPtr hdc, IntPtr hpal, ref NativeMethods.BITMAPINFO_FLAT pbmi)
        {
            IntPtr hbm  = IntPtr.Zero;
            bool   bRet = false;

            try
            {
                // Create a dummy bitmap from which we can query color format info
                // about the device surface.
                hbm = SafeNativeMethods.CreateCompatibleBitmap(new HandleRef(null, hdc), 1, 1);

                if (hbm == IntPtr.Zero)
                {
                    throw new OutOfMemoryException(SR.GraphicsBufferQueryFail);
                }

                pbmi.bmiHeader_biSize = Marshal.SizeOf(typeof(NativeMethods.BITMAPINFOHEADER));
                pbmi.bmiColors        = new byte[NativeMethods.BITMAPINFO_MAX_COLORSIZE * 4];

                // Call first time to fill in BITMAPINFO header.
                SafeNativeMethods.GetDIBits(new HandleRef(null, hdc),
                                            new HandleRef(null, hbm),
                                            0,
                                            0,
                                            IntPtr.Zero,
                                            ref pbmi,
                                            NativeMethods.DIB_RGB_COLORS);

                if (pbmi.bmiHeader_biBitCount <= 8)
                {
                    bRet = FillColorTable(hdc, hpal, ref pbmi);
                }
                else
                {
                    if (pbmi.bmiHeader_biCompression == NativeMethods.BI_BITFIELDS)
                    {
                        // Call a second time to get the color masks.
                        SafeNativeMethods.GetDIBits(new HandleRef(null, hdc),
                                                    new HandleRef(null, hbm),
                                                    0,
                                                    pbmi.bmiHeader_biHeight,
                                                    IntPtr.Zero,
                                                    ref pbmi,
                                                    NativeMethods.DIB_RGB_COLORS);
                    }
                    bRet = true;
                }
            }
            finally
            {
                if (hbm != IntPtr.Zero)
                {
                    SafeNativeMethods.DeleteObject(new HandleRef(null, hbm));
                    hbm = IntPtr.Zero;
                }
            }
            return(bRet);
        }