Guidance:
-- Display BMP bitmap in common mode, which occupies a large memory and is slow. When the image is reduced, the distortion is serious, A large distortion occurs when a high-color figure is displayed on a low-color device. This article uses the video function to display BMP bitmap, which can eliminate the above disadvantages.
---- 1. BMP file structure
---- 1. BMP file Composition
---- A bmp file consists of four parts: File Header, bitmap information header, color information, and graphic data.
---- 2. BMP File Header
---- The data structure of the BMP file header contains information such as the type, size, and start position of the BMP file.
---- Its structure is defined as follows:
Typedef struct tagbitmapfileheader
{
Wordbftype; // The type of the bitmap file, which must be BM.
DWORD bfsize; // the size of the bitmap file, in bytes.
Wordbfreserved1; // reserved word for bitmap files, which must be 0
Wordbfreserved2; // reserved word for bitmap files, which must be 0
DWORD bfoffbits; // the starting position of the bitmap data, relative to the bitmap
// The offset of the file header, in bytes
} Bitmapfileheader;
---- 3. Bitmap header ----
The BMP Bitmap header is used to describe the size and other information of the bitmap.
Typedef struct tagbitmapinfoheader {
DWORD bisize; // number of bytes occupied by the structure
Longbiwidth; // The width of the bitmap, in pixels.
Longbiheight; // The height of the bitmap, in pixels
Word biplanes; // The level of the target device, which must be 1
Word bibitcount // The number of digits required for each pixel, which must be 1 (two-color ),
// 4 (16 colors), 8 (256 colors), or 24 (true color)
DWORD bicompression; // bitmap compression type, which must be 0 (not compressed ),
// 1 (bi_rle8 compression type) or 2 (bi_rle4 compression type)
DWORD bisizeimage; // bitmap size, in bytes
Longbixpelspermeter; // horizontal bitmap resolution, number of workers per meter
Longbiypelspermeter; // bitmap vertical resolution, number of workers per meter
DWORD biclrused; // number of colors in the color table used by the bitmap
DWORD biclrimportant; // number of important colors in the bitmap display process
} Bitmapinfoheader;
---- 4. Color Table
---- The color table is used to describe the color in the bitmap. It has several table items. Each table item is an rgbquad-type structure and defines a color. The rgbquad structure is defined as follows:
Typedef struct tagrgbquad {
Bytergbblue; // blue brightness (value range: 0-255)
Bytergbgreen; // The brightness of the green color (value range: 0-255)
Bytergbred; // The Red brightness (value range: 0-255)
Bytergbreserved; // reserved, must be 0
} Rgbquad;
The number of rgbquad structure data in the color table is determined by bibitcount:
When bibitcount is 1, 4, and 8, there are 2, 16, and 256 table items respectively;
When bibitcount = 24, there is no color table item.
The bitmap information header and the color table form bitmap information. The bitmapinfo structure is defined as follows:
Typedef struct tagbitmapinfo {
Bitmapinfoheader bmiheader; // Bitmap header
Rgbquad bmicolors [1]; // color table
} Bitmapinfo;
---- 5. bitmap data
---- Bitmap data records each pixel value of the bitmap. The record sequence is from left to right in the scan row, and the rows are from bottom to top. The number of bytes occupied by a pixel value of a bitmap:
When bibitcount = 1, 8 pixels constitute 1 byte;
When bibitcount = 4, 2 pixels constitute 1 byte;
When bibitcount = 8, 1 pixel occupies 1 byte;
When bibitcount = 24, one pixel occupies three bytes;
Windows requires that the number of bytes occupied by a scan row must be
A multiple of 4 (in the unit of long). The insufficient values are filled with 0,
Calculation of the number of bytes occupied by a scanned row:
Datasizeperline = (biwidth * bibitcount + 31)/8;
// The number of bytes that a scan row occupies
Datasizeperline = datasizeperline/4*4; // The number of bytes must be a multiple of 4
Bitmap data size (without compression ):
Datasize = datasizeperline * biheight;
---- II. Common display method of BMP bitmap
---- 1. Apply for memory space for storing bitmap files
---- Globalalloc (ghnd, filelength );
---- 2. Reading bitmap files into the applied memory space
---- Loadfiletomemory (mpbitssrc, mfilename );
---- 3. Create a display bitmap in functions such as onpaint
---- Use createdibitmap () to create a display bitmap, and use createcompatibledc () to create compatible DC,
---- Use selectbitmap () to select a display bitmap.
---- 4. Use bitblt or stretchblt and other functions to display bitmap
---- 5. Use deleteobject () to delete the created bitmap
---- The disadvantage of the above method is: 1) Slow display speed; 2) large memory usage; 3) large image distortion when bitmap is reduced, (can be solved by installing the font smoothing software); 4) high-color (such as true color) image distortion is displayed on low-color devices (such as 256 display mode.
---- 3. BMP bitmap zoom display
---- Use the drawdib video function to display the bitmap. The memory usage is small and the speed is fast. In addition, the image can be deplayed (dithering. Desalination is a graphic algorithm used to display color images on a device that supports less colors than images. The BMP bitmap is displayed as follows:
---- 1. Open the video function drawdibopen (), which is generally placed in the constructor.
---- 2. Apply for memory space for storing bitmap files
---- Globalalloc (ghnd, filelength );
---- 3. Reading bitmap files into the applied memory space
---- Loadfiletomemory (mpbitssrc, mfilename );
---- 4. Use drawdibrealize () and drawdibdraw () to display bitmap in functions such as onpaint.
---- 5. Disable the video function drawdibclose (), which is generally placed in the destructor.
---- Advantages of the above methods: 1) fast display speed; 2) low memory usage; 3) Low image distortion during zoom display; 4) low Distortion when displaying high-color graphics on devices with low-color digits; 5) simple animation can be made by directly processing bitmap data.
---- Key Points of cviewbimap Programming
---- 1. Add video functions and other Members to the cviewbimap class
Hdrawdib m_hdrawdib; // video Function
Handlemhbitssrc; // bitmap file handle (memory)
Lpstr mpbitssrc; // bitmap file address (memory)
Bitmapinfoheader * mpbitmapinfo; // Bitmap header
---- 2. Add the video function to the cviewbimap class constructor.
---- M_hdrawdib = drawdibopen ();
---- 3. Add the function to disable the video in the cviewbimap class destructor.
If (m_hdrawdib! = NULL)
{
Drawdibclose (m_hdrawdib );
M_hdrawdib = NULL;
}
---- 4. Add graphicdraw () to the image display function onpaint of the cviewbimap class ()
Voidcviewbitmap: onpaint ()
{
Cpaintdc DC (this); // device context for painting
Graphicdraw ();
}
Voidcviewbitmap: graphicdraw (void)
{
Cclientdc DC (this); // device context for painting
Bitmapfileheader * pbitmapfileheader;
Ulong bfoffbits = 0;
Cpoint WID;
// The image file name is valid (= 0 BMP)
If (mbitmapfiletype // the image file name is valid (= 0 BMP)
// Prepare to display the true color bitmap
Pbitmapfileheader = (bitmapfileheader *) mpbitssrc;
Bfoffbits = pbitmapfileheader-> bfoffbits;
// Use a common function to display a bitmap
If (m_hdrawdib = NULL | mdispmethod = 0)
{
Hbitmap =: createdibitmap (DC. m_hdc,
Mpbitmapinfo, cbm_init, mpbitssrc + bfoffbits,
(Lpbitmapinfo) mpbitmapinfo, dib_rgb_colors );
// Create a bitmap
HDC hmemdc =: createcompatibledc (DC. m_hdc); // Create memory
Hbitmap hbitmapold = selectbitmap (hmemdc, hbitmap); // select an object
// The member crect mdispr is used to indicate the size of the graphic display area.
// The member cpoint MPOs is used to indicate the starting position coordinate of the graphic display.
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. Height ();
If (MPOs. X if (MPOs. Y if (mfullviewtog = 0)
{
// Display the 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 );
}
// Display the true color bitmap
: Deleteobject (SelectObject (hmemdc, hbitmapold ));
// Delete a bitmap
} Else {
// Use the video function to display bitmap
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. Height ();
If (MPOs. X if (MPOs. y // display the true color bitmap
Drawdibrealize (m_hdrawdib, DC. getsafehdc (), true );
If (mfullviewtog = 0)
{
WID. x = mdispr. Width ();
WID. Y = mdispr. Height ();
// When is displayed, the image size cannot be greater
If (WID. x> mpbitmapinfo-> biwidth)
WID. x = mpbitmapinfo-> biwidth;
If (WID. Y> mpbitmapinfo-> biheight)
WID. Y = mpbitmapinfo-> biheight;
Drawdibdraw (m_hdrawdib, DC. getsafehdc ()
, 0, 0, wid. X, wid. y,
Mpbitmapinfo, (lpvoid) (mpbitssrc + bfoffbits ),
MPOs. X, MPOs. Y, wid. X, wid. Y, ddf_backgroundpal );
} Else {
Drawdibdraw (m_hdrawdib, DC. getsafehdc (),
0, 0, mdispr. Width (), mdispr. Height (),
Mpbitmapinfo, (lpvoid) (mpbitssrc + bfoffbits ),
0, 0, mpbitmapinfo-> biwidth, mpbitmapinfo-> biheight,
Ddf_backgroundpal );
}
}
Return;
}
---- 5. Use the cviewbimap class to display BMP bitmaps
---- 1. Create a new project file named mymap in Visual C ++ 5.0 with the type of MFC Appwizard [EXE]. After compilation and running are passed, click resourceview In workspace (such as closed, open with alt_0), click the + symbol on the left of menu to expand the menu entry, and double-click the idr_mainframe entry to go to the menu resource editing page, add the "viewbitmap" entry at the end of the "View (v)" drop-down menu (view drop-down menu in English Version). Its ID is id_view_bitmap.
---- 2. in Visual C ++ 5.0, click the drop-down menu project> Add to project> files... to add bitmap0.h and bitmap0.cpp to the project file.
---- 3. in Visual C ++ 5.0, press ctrl_w to enter the MFC classwizard, select the class name cmainframe, objectids: id_view_bitmap, messages SELECT command, then click Add fucction, and enter the function name onviewbimap. After onviewbimap is added, click onviewbimap entry in member functions: and click Edit code to edit the program code. The Code is as follows:
Void cmainframe: onviewbitmap ()
{
// Todo: add your command handler code here
Cviewbitmap * pviewbitmap = NULL;
Pviewbitmap = new cviewbitmap ("bitmap. BMP", this );
Pviewbitmap-> showwindow (true );
}
---- Add # include "bitmap0.h" in the program header, and then compile and run the program.
---- 4. Find a larger real-color BMP bitmap and copy it to bitmap. BMP.
---- 5. During running, click View (V)-> viewbitmap (view in English) to display bitmap. BMP bitmap.
---- 6. cviewbimap Functions
---- 1. The client area contains horizontal and vertical scroll bars. You can use a scroll bar when the bitmap size is greater than the client area. When the bitmap size is smaller than the client area or full screen display, the scroll bar is invalid.
---- 2. There is a status bar at the bottom of the customer area. The first in the status bar is the bitmap information, and the second is the bitmap display method, which can be a common function or a video function. Click the mouse in the second area to switch between the two. The third is the bitmap display ratio, which can be 1; 1 or full screen. In the third frame, click the mouse to switch between the two. In full screen display, if the bitmap is smaller than the customer area, the bitmap is enlarged. If the bitmap is larger than the customer area, the bitmap is reduced.
---- 3. Supports file drag and drop. You can drag a bitmap file from the Resource Manager to the customer area to display the bitmap.
---- After the program passes debugging, you can find a large real-color bitmap or adjust the customer area to be smaller than the bitmap. In the full-screen display mode, the difference between using common functions and using video functions is compared. It can be seen that there is little difference between the two when the bitmap is enlarged, but there is a significant difference between the two when the bitmap is reduced. When the video function is used, the bitmap distortion is small and the display speed is fast.
---- You can also switch the screen display mode from the true color display mode to the 256 color display mode from the control panel, and then compare the difference between using a common function and using a video function to display the same true color bitmap. Now we can see the advantages of using video functions.
---- When the bitmap is displayed in full screen, the XY ratio of the bitmap is different. To maintain the same proportion, you can adjust it in the display program.
This article is transferred from
Http://www.newasp.net/tech/program/20623.html