Exemplo n.º 1
0
        /// <summary>
        /// Creates a 32 bit HBITMAP of the specified size.
        /// </summary>
        /// <param name="hdc">The HDC.</param>
        /// <param name="size">The size.</param>
        /// <param name="bits">The bits.</param>
        /// <param name="hBitmap">The bitmap handle.</param>
        /// <returns>True if the bitmap was created successfully.</returns>
        private static bool Create32BitHBITMAP(IntPtr hdc, Size size, out IntPtr bits, out IntPtr hBitmap)
        {
            //  Create a bitmap info setup for a 32 bit bitmap.
            var bi = new BITMAPINFO
                     {
                         bmiHeader = new BITMAPINFOHEADER
                                     {
                                         biSize = (uint) Marshal.SizeOf(typeof (BITMAPINFOHEADER)),
                                         biPlanes = 1,
                                         biCompression = (uint) BI.BI_RGB,
                                         biWidth = size.Width,
                                         biHeight = size.Height,
                                         biBitCount = 32
                                     }
                     };

            //  Create the DIB section.
            hBitmap = Gdi32.CreateDIBSection(hdc, ref bi, (uint)DIB.DIB_RGB_COLORS, out bits, IntPtr.Zero, 0);

            //  Return success only if we have a handle and bitmap bits.
            return hBitmap != IntPtr.Zero && bits != IntPtr.Zero;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts raw image data into PARGB32.
        /// </summary>
        /// <param name="hdc">The handle to the device context..</param>
        /// <param name="pargb">The pixel data.</param>
        /// <param name="hbmp">The bitmap handle.</param>
        /// <param name="imageSize">The image size.</param>
        /// <param name="rowLength">The row length.</param>
        private unsafe static void ConvertToPARGB32(IntPtr hdc, UInt32* pargb, IntPtr hbmp, Size imageSize, int rowLength)
        {
            var bmi = new BITMAPINFO
            {
                bmiHeader = new BITMAPINFOHEADER
                {
                    biSize = (uint) Marshal.SizeOf(typeof (BITMAPINFOHEADER)),
                    biPlanes = 1,
                    biCompression = (uint) BI.BI_RGB,
                    biWidth = imageSize.Width,
                    biHeight = imageSize.Height,
                    biBitCount = 32
                }
            };

            //  Allocate data sufficient for the pixel data.
            IntPtr hHeap = Kernel32.GetProcessHeap();
            void* pvBits = Kernel32.HeapAlloc(hHeap, 0, new UIntPtr((uint)(bmi.bmiHeader.biWidth * 4 * bmi.bmiHeader.biHeight))).ToPointer();
            if (pvBits == (void*) 0)
                return;
            
            //  Get the bitmap bits.
            var ptr = new IntPtr(pvBits);
            if (Gdi32.GetDIBits(hdc, hbmp, 0, (uint) bmi.bmiHeader.biHeight, ref ptr,ref  bmi, (uint)DIB.DIB_RGB_COLORS) ==
                bmi.bmiHeader.biHeight)
            {
                //  Now handle each pixel.
                UInt32 cxDelta = (uint)(rowLength - bmi.bmiHeader.biWidth);
                UInt32* pargbMask = (uint*)pvBits;

                for (UInt32 y = (UInt32) bmi.bmiHeader.biHeight; y != 0; --y)
                {
                    for (UInt32 x = (UInt32) bmi.bmiHeader.biWidth; x != 0; --x)
                    {
                        if ((*pargbMask++) != 0)
                        {
                            // transparent pixel
                            *pargb++ = 0;
                        }
                        else
                        {
                            // opaque pixel
                            *pargb++ |= 0xFF000000;
                        }
                    }

                    pargb += cxDelta;
                }
            }

            Kernel32.HeapFree(hHeap, 0, new IntPtr(pvBits));
        }
Exemplo n.º 3
0
 public static extern int GetDIBits([In] IntPtr hdc, [In] IntPtr hbmp, uint uStartScan, uint cScanLines, ref IntPtr lpvBits, ref BITMAPINFO lpbi, uint uUsage);