Ejemplo n.º 1
0
            // bFillBitmapInfo
            //
            // 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.
            //
            // Returns:
            //   TRUE if successful, FALSE otherwise.
            //
            // History:
            //  07-Jun-1995 -by- Gilman Wong [gilmanw]
            // Wrote it.
            //
            //  15-Nov-2000 -by- Chris Anderson [chrisan]
            // Ported it to C#
            //
            private bool bFillBitmapInfo(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.GetString(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.
                    //

                    IntPtr diRet = 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 = bFillColorTable(hdc, hpal, ref pbmi);
                    }
                    else
                    {
                        if (pbmi.bmiHeader_biCompression == NativeMethods.BI_BITFIELDS)
                        {
                            //
                            // Call a second time to get the color masks.
                            // It's a GetDIBits Win32 "feature".
                            //

                            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);
            }