The BMP bitmap is displayed in a common method, which is large, slow, when the graphic is reduced, the distortion is severe, and the graphic graphic of the high color bits is displayed on the device of the low color digit. This article uses a video function to display BMP bitmaps, which can eliminate the above shortcomings.
---- One, BMP file structure
---- 1. BMP file composition
---- BMP file consists of file head, bitmap information head, color information, and graphics data.
---- 2. BMP file header --- BMP file header data structure contains information such as the type of BMP file, file size, and bitmap starting position. ---- The structure is defined as follows:
Typedef struct tagbitmapfileheader {
WordbfType; // Bitmap file type, must be BM
DWORD BFSIZE; // Bitmap file size, byte
Wordbfreserved1; // bitmap file reserved word, must be 0
Wordbfreserved2; // bitmap file reserved word, must be 0
DWORD BFOFFBITS; // Bit map data starting position to relatively
// The offset representation of the file header is in byte} BitmapFileHeader;
---- 3. Bitmap information head
---- BMP bitmap information header data is used to explain the size of the bitmap.
Typedef struct tagbitmapinfoheader {dWord bisize; // This structure occupies bytes
Longbiwidth; // Bit map width, in pixels
Longbiheight; // bit map height, in pixels
Word biplanes; // The level of target device must be 1
Word Bibitcount // The number of bits required for each pixel must be 1 (two-color),
// 4 (16 colors), 8 (256 colors) or 24 (true color)
DWORD BICOMPRESSION; // bitmap compression type, must be 0 (not compressed),
// 1 (Bi_RLE8 compression type) or 2 (Bi_RLE4 compression type)
DWORD BisizeImage; // Bit map size, byte
Longbixpelspermeter; // bitmap level resolution, number of pixels per meter
Longbiypelspermeter; // bitmap vertical resolution, number of pixels per meter
DWORD biclrused; // number of color tables in the color table actually used by the bitmap
DWORD BICLRIMPORTANT; // Bitchart Display Direct Color Count} BitmapInfoHeader;
---- 4. Color table
---- Color table is used to explain the color in the bitmap, which has several entries, each entry is a RGBQUAD type structure, defining a color. The RGBQUAD structure is defined as follows: typedef struct tagrgbquad {bytergbblue; // Blue brightness (value range is 0-255)
Bytergbgreen; // Green brightness (value range is 0-255)
Bytergbred; // red brightness (value range is 0-255)
Bytergbreserved; // Reserved, must be 0} RGBQUAD;
The number of RGBQUAD structure data in color table has BIBITCOUNT to determine: When BiBitCount = 1, 4, 8, there are 2, 16,256 entries, respectively; when BiBitCount = 24, there is no color entries. The bitmap information head and color table form bitmap information, the BitmapInfo structure is defined as follows:
Typedef struct tagbitmapinfo {bitmapinfoheader bmiheader; // bitmap information head
RGBQUAD BMICOLORS [1]; // Color table} BitmapInfo;
---- 5. Bitmap data
---- bitmap data records each pixel value of the bitmap, the recording order is from left to right in the scan line, and the scan line is from bottom to. One byte number of pixel values of bitmaps: When BiBitCount = 1, 8 pixels account for 1 byte; when BiBitCount = 4, 2 pixels account for 1 byte; when BiBitcount = 8, 1 Pixels account for 1 byte; when BiBitCount = 24, one pixel accounts for 3 bytes; Windows specifies the number of bytes occupied by a scan line must be a multiple of 4 (ie, in units of long), insufficient 0 Fill, a scan line accounts for the number of bytes calculation method: DataSizePerLine = (BiWidth * BibitCount 31) / 8; // A scan line occupies the number of bytes
DataSizePerLine = DataSizePerLine / 4 * 4; // The number of bytes must be the size of the multiple bitmap data (no compression): DataSize = DataSizePerline * Biheight;
---- II, BMP bitmap general display method
---- 1. Apply for memory space for storage bitmap file
---- GlobalLoc (GHND, FileLength);
---- 2. Bitmap file read in the application memory space
---- LoadFileTomeMory (MPBITSRC, MFileName);
---- 3. In the onpaint function, create a display bitmap
---- Create a display bitmap with createDibitMap, create a compatible DC with CreateCompatibleDC (),
---- Select the display bitmap with SelectBitMap ().
---- 4. Display bitmaps with functions such as Bitblt or StretchBLT
---- 5. Delete the created bitmap with deleteObject ()
---- The shortcomings of the above method are: 1) Slow display speed; 2) Memory occupation is large; 3) The bitmap is large when the display is large, (can be solved by installing font smoothing software); 4) On the device (such as 256 display mode) shows the graphic distortion of the high color bits (such as true color) graphics.
---- Third, BMP bit map scale display
---- Use the DrawDib video function to display bitmap, with less memory, fast speed, and DITHERING processing. Duel processing is a graphics algorithm that can be used to display color images on a device that supports less color than the image. The BMP bitmap display method is as follows:
---- 1. Turn on the video function DrawDibopen (), generally placed in the constructor
---- 2. Apply for memory space for storage bitmap file
---- GlobalLoc (GHND, FileLength);
---- 3. Bitmap file read into the application memory space
---- LoadFileTomeMory (MPBITSRC, MFileName);
---- 4. Use DrawDibRealize (), DrawDibDraw () display bitmap in onpaint functions.
---- 5. Close the video function DrawDibClose (), generally placed in the destructor
---- The advantages of the above method are: 1) Fast display speed; 2) Demolition is less; 3) Time of graphic distortion, 4) When the low color bit number is displayed, the graphic graphics of the high color bit Small distortion; 5) You can make simple animations by direct processing bitmap data.
---- Four, CVIEWBIMAP class programming points
---- 1. Add video functions such as video functions in the CViewBIMAP class
HDrawDIB M_HDRAWDIB; // Video Function
HandleMhbitssrc; // bitmap file handle (memory)
LPSTR MPBITSSRC; // bitmap file address (memory)
BitmapInfoHeader * mpbitmapinfo; // bitmap information head
---- 2. Add to open the video function in the CVIEWBIMAP class constructor
---- m_hdrawdib = DrawDibopen (); ---- 3. Add a shutdown video function in the CVIEWBIMAP classification function
IF (m_hdrawdib! = null) {drawdibclose (m_hdrawdib); m_hdrawdib = NULL;}
---- 4. Display graphics functions in CViewBimap class OnPaint added GraphicDraw () voidCViewBitmap :: OnPaint () {CPaintDC dc (this); // device context for painting GraphicDraw ();} voidCViewBitmap :: GraphicDraw (void) { CclientDC DC (this); // Device Context for Painting BitmapfileHeader * PbitmapfileHead;
Ulong bfoffbits = 0;
Cpoint wid; // graphic file name is valid (= 0 bmp)
IF (MbitmapFileType // Prepare to display true color level map PbitmapfileHeader = (BitmapfileHeader *) MPBITSSRC; BfoffBits = PbitmapfileHeader-> bfoffbits; // Display bitmap using a normal function if (m_hDrawDib == NULL || mDispMethod == 0) {HBITMAP hBitmap = :: CreateDIBitmap (dc.m_hDC, mpBitmapInfo, CBM_INIT, mpBitsSrc bfoffBits, (LPBITMAPINFO) mpBitmapInfo, DIB_RGB_COLORS); // build bitmap HDC Hmemdc = :: createcompatibledc (dc.m_hdc); // Establish memory HBitmap hbitmapold = selectbitmap (hmemdc, hbitmap); // Select an object // Member CRECT MDISPR is used to indicate the size of the graphic display area. // Member CPOINT MPOS is used to indicate graphical display start position coordinates. IF (mpos.x> (mpbitmapinfo-> biwidth - mdispr.width ()))) Mpos.x = mpbitmapinfo-> biwidth - mdispr.width (); IF (mpos.y> (mpbitmapinfo-> biheight- mdispr.Height ()))) Mpos.y = mpbitmapinfo-> biheight- mdispr.h EIGHT (); IF (Mpos.x <0) mpos.x = 0; IF (mpos.y <0) mpos.y = 0; IF (mfullviewtog == 0) {// Display true color bitmap :: Bitblt (Dc.m_hdc, 0,0, mdispr.width (), mdispr.height (), HMEMDC, Mpos.x, Mpos.y, Srccopy } Else {:: Stretchblt (dc.m_hdc, 0,0, mdispr.width (), mdispr.height (), hmemdc, 0,0, mpbitmapinfo-> biwidth, mpbitmapinfo-> biheight, srccopy; // End display true color level map