Esempio n. 1
0
        /// <summary>
        /// Converts a GDI+ image to a Device Independent Bitmap. 
        /// </summary>
        /// <param name="image">The image that shall be converted</param>
        /// <param name="bitmapInfoPointer">Pointer to the BitmapInfo Structure of the Dib</param>
        /// <param name="bitmapBitsPointer">Pointer to the pixel of the Dib</param>
        public static void ImageToDib(
            Image image,
            ref IntPtr bitmapInfoPointer,
            ref IntPtr bitmapBitsPointer)
        {
            if (image!=null)
            {
                //Get device context and bitmap handle
                IntPtr compatibleDC=CreateCompatibleDC(IntPtr.Zero);
                IntPtr bitmapHandle=new System.Drawing.Bitmap(image).GetHbitmap();

                //Receive BitmapInfo from image
                BitmapInfo bitmapInfo=new BitmapInfo();

                bitmapInfoPointer=Marshal.AllocHGlobal(Marshal.SizeOf(bitmapInfo));

                Marshal.StructureToPtr(
                    bitmapInfo,
                    bitmapInfoPointer, true);

                Gdi32.GetDIBits(
                    compatibleDC,
                    bitmapHandle,
                    0,
                    0,
                    IntPtr.Zero,
                    bitmapInfoPointer,
                    ColorUse.DibRGBColors);

                Marshal.PtrToStructure(
                    bitmapInfoPointer,
                    bitmapInfo);

                //Allocate memory for the bitmapbits and receive them
                Int32 s=((((bitmapInfo.bmiHeader.biWidth*bitmapInfo.bmiHeader.biBitCount)+31)/32)*4)*
                        System.Math.Abs(bitmapInfo.bmiHeader.biHeight);

                bitmapBitsPointer=Marshal.AllocHGlobal(bitmapInfo.bmiHeader.biSizeImage+1024);

                Gdi32.GetDIBits(
                    compatibleDC,
                    bitmapHandle,
                    0,
                    (uint)image.Height,
                    bitmapBitsPointer,
                    bitmapInfoPointer,
                    ColorUse.DibRGBColors);

                //Tidy up
                Gdi32.DeleteObject(bitmapHandle);
                Gdi32.DeleteDC(compatibleDC);
            }
        }
Esempio n. 2
0
 public static extern Int32 GetDIBits(
     IntPtr DeviceContext,
     IntPtr BitmapHandle,
     uint StartScanLine,
     uint ScanLines,
     IntPtr BitmapBits,
     ref BitmapInfo bmi,
     ColorUse Usage);