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);
        }
        private IntPtr CreateCompatibleDIB(IntPtr hdc, IntPtr hpal, int ulWidth, int ulHeight, ref IntPtr ppvBits)
        {
            if (hdc == IntPtr.Zero)
            {
                throw new ArgumentNullException("hdc");
            }
            IntPtr zero = IntPtr.Zero;

            System.Drawing.NativeMethods.BITMAPINFO_FLAT pbmi = new System.Drawing.NativeMethods.BITMAPINFO_FLAT();
            switch (System.Drawing.UnsafeNativeMethods.GetObjectType(new HandleRef(null, hdc)))
            {
            case 3:
            case 4:
            case 10:
            case 12:
                if (this.bFillBitmapInfo(hdc, hpal, ref pbmi))
                {
                    pbmi.bmiHeader_biWidth  = ulWidth;
                    pbmi.bmiHeader_biHeight = ulHeight;
                    if (pbmi.bmiHeader_biCompression == 0)
                    {
                        pbmi.bmiHeader_biSizeImage = 0;
                    }
                    else if (pbmi.bmiHeader_biBitCount == 0x10)
                    {
                        pbmi.bmiHeader_biSizeImage = (ulWidth * ulHeight) * 2;
                    }
                    else if (pbmi.bmiHeader_biBitCount == 0x20)
                    {
                        pbmi.bmiHeader_biSizeImage = (ulWidth * ulHeight) * 4;
                    }
                    else
                    {
                        pbmi.bmiHeader_biSizeImage = 0;
                    }
                    pbmi.bmiHeader_biClrUsed      = 0;
                    pbmi.bmiHeader_biClrImportant = 0;
                    zero = SafeNativeMethods.CreateDIBSection(new HandleRef(null, hdc), ref pbmi, 0, ref ppvBits, IntPtr.Zero, 0);
                    Win32Exception exception = null;
                    if (zero == IntPtr.Zero)
                    {
                        exception = new Win32Exception(Marshal.GetLastWin32Error());
                    }
                    if (exception != null)
                    {
                        throw exception;
                    }
                }
                return(zero);
            }
            throw new ArgumentException(System.Drawing.SR.GetString("DCTypeInvalid"));
        }
        private unsafe bool bFillColorTable(IntPtr hdc, IntPtr hpal, ref System.Drawing.NativeMethods.BITMAPINFO_FLAT pbmi)
        {
            bool flag = false;

            byte[] lppe = new byte[sizeof(System.Drawing.NativeMethods.PALETTEENTRY) * 0x100];
            fixed(byte *numRef = pbmi.bmiColors)
            {
                fixed(byte *numRef2 = lppe)
                {
                    System.Drawing.NativeMethods.RGBQUAD *     rgbquadPtr      = (System.Drawing.NativeMethods.RGBQUAD *)numRef;
                    System.Drawing.NativeMethods.PALETTEENTRY *paletteentryPtr = (System.Drawing.NativeMethods.PALETTEENTRY *)numRef2;
                    int nEntries = ((int)1) << pbmi.bmiHeader_biBitCount;

                    if (nEntries <= 0x100)
                    {
                        uint   num3;
                        IntPtr zero = IntPtr.Zero;
                        if (hpal == IntPtr.Zero)
                        {
                            zero = Graphics.GetHalftonePalette();
                            num3 = SafeNativeMethods.GetPaletteEntries(new HandleRef(null, zero), 0, nEntries, lppe);
                        }
                        else
                        {
                            num3 = SafeNativeMethods.GetPaletteEntries(new HandleRef(null, hpal), 0, nEntries, lppe);
                        }
                        if (num3 != 0)
                        {
                            for (int i = 0; i < nEntries; i++)
                            {
                                rgbquadPtr[i].rgbRed      = paletteentryPtr[i].peRed;
                                rgbquadPtr[i].rgbGreen    = paletteentryPtr[i].peGreen;
                                rgbquadPtr[i].rgbBlue     = paletteentryPtr[i].peBlue;
                                rgbquadPtr[i].rgbReserved = 0;
                            }
                            flag = true;
                        }
                    }
                }
            }

            return(flag);
        }
 private IntPtr CreateCompatibleDIB(IntPtr hdc, IntPtr hpal, int ulWidth, int ulHeight, ref IntPtr ppvBits)
 {
     if (hdc == IntPtr.Zero)
     {
         throw new ArgumentNullException("hdc");
     }
     IntPtr zero = IntPtr.Zero;
     System.Drawing.NativeMethods.BITMAPINFO_FLAT pbmi = new System.Drawing.NativeMethods.BITMAPINFO_FLAT();
     switch (System.Drawing.UnsafeNativeMethods.GetObjectType(new HandleRef(null, hdc)))
     {
         case 3:
         case 4:
         case 10:
         case 12:
             if (this.bFillBitmapInfo(hdc, hpal, ref pbmi))
             {
                 pbmi.bmiHeader_biWidth = ulWidth;
                 pbmi.bmiHeader_biHeight = ulHeight;
                 if (pbmi.bmiHeader_biCompression == 0)
                 {
                     pbmi.bmiHeader_biSizeImage = 0;
                 }
                 else if (pbmi.bmiHeader_biBitCount == 0x10)
                 {
                     pbmi.bmiHeader_biSizeImage = (ulWidth * ulHeight) * 2;
                 }
                 else if (pbmi.bmiHeader_biBitCount == 0x20)
                 {
                     pbmi.bmiHeader_biSizeImage = (ulWidth * ulHeight) * 4;
                 }
                 else
                 {
                     pbmi.bmiHeader_biSizeImage = 0;
                 }
                 pbmi.bmiHeader_biClrUsed = 0;
                 pbmi.bmiHeader_biClrImportant = 0;
                 zero = SafeNativeMethods.CreateDIBSection(new HandleRef(null, hdc), ref pbmi, 0, ref ppvBits, IntPtr.Zero, 0);
                 Win32Exception exception = null;
                 if (zero == IntPtr.Zero)
                 {
                     exception = new Win32Exception(Marshal.GetLastWin32Error());
                 }
                 if (exception != null)
                 {
                     throw exception;
                 }
             }
             return zero;
     }
     throw new ArgumentException(System.Drawing.SR.GetString("DCTypeInvalid"));
 }