# Include <windows. h> // contains the definition structure of the relevant BMP format <br/> # include <stdio. h> <br/> # include <stdlib. h> </P> <p> int BMP width; // BMP image width (pixels) <br/> int bmpheight; // BMP Image Height (pixels) <br/> rgbquad * pcolortable; // color palette array (not used in true color) <br/> int bibitcount; // bytes occupied by each pixel </P> <p> /************************* ********************************** <br/> * Name: read_bmp <br/> * function: Read the BMP image and return the read hexadecimal array <br/> * entry parameter: BMP filename: BMP image name <br/> * exit parameter: none <br/> *********************************** * *************************/<br/> unsigned char * read_bmp (const char * BMP filename) <br/>{< br/> unsigned char * pbmpbuf; // storage buffer <br/> file * fp = fopen (BMP filename, "rb "); // enable the binary system <br/> If (FP = NULL) <br/> {<br/> printf ("can not open this file "); <br/> return 0; <br/>}< br/> fseek (FP, sizeof (bitmapfileheader), 0); // skip the BMP header <br/> bitmapinfoheader infoheader; <br/> fread (& infoheader, sizeof (bitmapinfoheader), 1, FP); // read BMP Image Information <br/> BMP width = infoheader. biwidth; <br/> bmpheight = infoheader. biheight; <br/> bibitcount = infoheader. bibitcount; <br/> int line_byte = (BMP width * bibitcount/8 + 3)/4*4; // The number of bytes in each row <br/> If (bibitcount = 8) <br/>{< br/> pcolortable = new rgbquad [256]; <br/> fread (pcolortable, sizeof (rgbquad), 256, FP ); <br/>}< br/> pbmpbuf = new unsigned char [line_byte * bmpheight]; // open the storage area <br/> fread (pbmpbuf, 1, line_byte * bmpheight, FP); <br/> fclose (FP); // close the file <br/> return pbmpbuf; <br/>}</P> <p> /*************************** * ********************************* <br/> * Name: main <br/> * function: test function <br/> * entry parameter: None <br/> * exit parameter: none <br/> *********************************** * **************************/<br/> int main () <br/>{< br/> unsigned char * buffer = read_bmp ("1.bmp"); // read BMP images <br/> file * fp = fopen ("test.txt ", "WB"); <br/> fwrite (buffer, 1, (BMP width * bibitcount/8 + 3)/4*4 * bmpheight, FP ); // write the read image data to test.txt <br/> return 0; <br/>}
There is a problem here: The hexadecimal data stored in the text file is garbled and needs to be read using a hexadecimal editing tool. By default, the BMP image reading sequence starts from the lower left corner, in this way, you need to scan data from the lower left corner or convert the data to the order starting from the upper left corner.