The basic structure of the bitmap file is described in msdn, which consists of four parts:
File Header: bitmapfileheader |
Information header: bitmapinfoheader |
Palette: rgbquad |
Data Segment |
When the pixel is greater than 8 bits, there is no color palette.
The following is the code.
Enter dwwidth, dwheight, wbitperpixel, and pbits.
Int I; int width = dwwidth; int Height = dwheight; int bitcount = wbitsperpixel; // The number of digits of a pixel: 1, 4, 8, 16, 24, 32 int Index = 0; unsigned char rgbquad [4]; bitmapfileheader; bitmapinfoheader; DWORD widthbytes = (width * bitcount + 31)/32) * 4; // each line is a multiple of 4 // The offset from the file header to the data, calculate the memory size of the palette switch (bitcount) {Case 1: Index = 2; bitmapfileheader. bfoffbits = (DWORD) (sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + 2*4); break; Case 4: Index = 16; bitmapfileheader. bfoffbits = (DWORD) (sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + 16*4); break; case 8: Index = 256; bitmapfileheader. bfoffbits = (DWORD) (sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + 256 * sizeof (rgbquad); break; Case 24: Case 32: Index = 0; bitmapfileheader. bfoffbits = (DWORD) (sizeof (bitmapfileheader) + sizeof (bitmapinfoheader); break; default: break;} // construct bitmap file header bitmapfileheader. bftype = 0x4d42; bitmapfileheader. bfreserved1 = 0; bitmapfileheader. bfreserved2 = 0; bitmapfileheader. bfsize = (DWORD) (bitmapfileheader. bfoffbits + height * widthbytes); // BMP file length // construct bitmap file information header bitmapinfoheader. bibitcount = bitcount; bitmapinfoheader. biclrimportant = 0; bitmapinfoheader. biclrused = 0; bitmapinfoheader. bicompression = bi_rgb; bitmapinfoheader. biheight = height; bitmapinfoheader. biwidth = width; bitmapinfoheader. biplanes = 1; bitmapinfoheader. bisize = sizeof (bitmapinfoheader); bitmapinfoheader. bisizeimage = height * widthbytes; bitmapinfoheader. bixpelspermeter = 3780; bitmapinfoheader. biypelspermeter = 3780; // create a BMP memory image byte * pmybmp = new byte [bitmapfileheader. bfsize];
Byte * curr = pmybmp; memset (curr, 0, bitmapfileheader. bfsize); // write header information memcpy (curr, & bitmapfileheader, sizeof (bitmapfileheader ));
curr=pMyBmp+sizeof(BITMAPFILEHEADER); memcpy(curr,&bitmapInfoHeader,sizeof(BITMAPINFOHEADER)); curr+=sizeof(BITMAPINFOHEADER);
// Construct the color palette if (bitcount = 8) {rgbquad [3] = 0; for (I = 0; I <index; I ++) {rgbquad [0] = rgbquad [1] = rgbquad [2] = I; memcpy (curr, rgbquad, sizeof (rgbquad); curr + = sizeof (rgbquad );}} else if (bitcount = 1) {rgbquad [3] = 0; for (I = 0; I <index; I ++) {rgbquad [0] = rgbquad [1] = rgbquad [2] = (256-I) % 256; memcpy (curr, rgbquad, sizeof (rgbquad )); curr + = sizeof (rgbquad) ;}// write the image data memcpy (curr, pbits, height * widthbytes); CLI :: array <byte> ^ BMP bytes2 = gcnew array <byte> (bitmapfileheader. bfsize); pin_ptr <byte> P2 = & BMP bytes2 [0];: memcpy (P2, pmybmp, BMP bytes2-> length * sizeof (byte )); // copy BMP data from an unmanaged space to a hosted space system: IO: memorystream ^ MS2 = gcnew system: IO: memorystream (BMP bytes2 ); // directly create an image class object from the Memory Data Stream Image = image: fromstream (MS2); // create an image class object from the data stream