Esempio n. 1
0
        static protected byte[] CreateEmptyBitmap(int width, int height, ushort bpp, uint imageSize, out uint headerSize)
        {
            BITMAPINFO bi = new BITMAPINFO();

            bi.biSize         = (uint)Marshal.SizeOf(bi);
            bi.biBitCount     = bpp; // bit depth
            bi.biClrUsed      = 0;
            bi.biClrImportant = 0;
            bi.biCompression  = 0; // BI_RGB
            bi.biHeight       = -height;
            bi.biWidth        = width;
            bi.biPlanes       = 1;
            bi.biSizeImage    = imageSize;

            BITMAPFILEHEADER bfh = new BITMAPFILEHEADER();

            uint Fsize = (uint)Marshal.SizeOf(bfh) + (uint)Marshal.SizeOf(bi) + imageSize;

            bfh.bfType    = 0x4D42;            // 'B' and 'M' bytes for BM type
            bfh.bfSize    = Fsize;             // total bitmap file size
            bfh.bfOffBits = Fsize - imageSize; // bitmap header offset

            headerSize = bfh.bfOffBits;

            //////////////////////////////////////////////////////////
            // create memory buffer that will hold the complete bitmap
            //////////////////////////////////////////////////////////
            byte[] buffer = new byte[Fsize];

            unsafe
            {
                fixed(byte *bPtr = buffer)
                {
                    IntPtr bfhPtr = new IntPtr((void *)bPtr);
                    IntPtr bihPtr = new IntPtr((void *)(bPtr + Marshal.SizeOf(bfh)));

                    ////////////////////////////////////////
                    //  write bitmap header to sream
                    ////////////////////////////////////////
                    Marshal.StructureToPtr(bfh, bfhPtr, true);

                    ////////////////////////////////////////
                    //  write bitmap info header to sream
                    ////////////////////////////////////////
                    Marshal.StructureToPtr(bi, bihPtr, true);
                }
            }

            return(buffer);
        }
Esempio n. 2
0
        protected static byte[] CreateEmptyBitmap(int width, int height, ushort bpp, uint imageSize, out uint headerSize)
        {
            BITMAPINFO bi = new BITMAPINFO();
            bi.biSize = (uint)Marshal.SizeOf(bi);
            bi.biBitCount = bpp; // bit depth
            bi.biClrUsed = 0;
            bi.biClrImportant = 0;
            bi.biCompression = 0; // BI_RGB
            bi.biHeight = -height;
            bi.biWidth = width;
            bi.biPlanes = 1;
            bi.biSizeImage = imageSize;

            BITMAPFILEHEADER bfh = new BITMAPFILEHEADER();

            uint Fsize = (uint)Marshal.SizeOf(bfh) + (uint)Marshal.SizeOf(bi) + imageSize;

            bfh.bfType = 0x4D42; // 'B' and 'M' bytes for BM type
            bfh.bfSize = Fsize; // total bitmap file size
            bfh.bfOffBits = Fsize - imageSize; // bitmap header offset

            headerSize = bfh.bfOffBits;

            //////////////////////////////////////////////////////////
            // create memory buffer that will hold the complete bitmap
            //////////////////////////////////////////////////////////
            byte[] buffer = new byte[Fsize];

            unsafe
            {
                fixed (byte* bPtr = buffer)
                {
                    IntPtr bfhPtr = new IntPtr((void*)bPtr);
                    IntPtr bihPtr = new IntPtr((void*)(bPtr + Marshal.SizeOf(bfh)));

                    ////////////////////////////////////////
                    //  write bitmap header to sream
                    ////////////////////////////////////////
                    Marshal.StructureToPtr(bfh, bfhPtr, true);

                    ////////////////////////////////////////
                    //  write bitmap info header to sream
                    ////////////////////////////////////////
                    Marshal.StructureToPtr(bi, bihPtr, true);
                }
            }

            return buffer;
        }