BMP file read/write review-C ++ implementation

Source: Internet
Author: User
Tags fread

It took several hours to read the format of the BMP file and write a small program...

BMP file format written good article: http://www.cnblogs.com/ZXYloveFR/archive/2012/08/06/2625225.html

BMP file format:

typedef struct tagBITMAPFILEHEADER {        WORD    bfType;        DWORD   bfSize;        WORD    bfReserved1;        WORD    bfReserved2;        DWORD   bfOffBits;} BITMAPFILEHEADER, FAR *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER;

 

typedef struct tagBITMAPINFOHEADER{        DWORD      biSize;        LONG       biWidth;        LONG       biHeight;        WORD       biPlanes;        WORD       biBitCount;        DWORD      biCompression;        DWORD      biSizeImage;        LONG       biXPelsPerMeter;        LONG       biYPelsPerMeter;        DWORD      biClrUsed;        DWORD      biClrImportant;} BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;

Main Code: (function: Read the file header, information header, and bitmap data from the BMP file .. Write the data to a BMP file defined by us .)

# Include <iostream> # include <windows. h> using namespace STD; void main () {file * stream = fopen ("D: \ 3.bmp", "rb"); If (Stream = NULL) {cout <"file does not exist" <Endl; return;} int sizefileheader = sizeof (bitmapfileheader); int sizeinfoheader = sizeof (bitmapinfoheader); Word filetype; DWORD filesize; DWORD dataoffset; bitmapfileheader * bitmapfileheader = new bitmapfileheader [sizefileheader + 1]; DWORD sizeimage = 0; bitmapinfoheader * bitma Pinfoheader = new bitmapinfoheader [sizeinfoheader + 1]; memset (bitmapfileheader, 0, sizefileheader + 1); memset (bitmapinfoheader, 0, sizeinfoheader + 1); fread (bitmapfileheader, sizeof (char), sizefileheader, stream); fseek (stream, sizefileheader, 0); fread (bitmapinfoheader, sizeof (char), sizeinfoheader, stream ); // ************************************** filetype = bitmapfileheader-> bftype; if (filetype! = 0x4d42) {cout <"This is not a BMP file" <Endl; return;} filesize = bitmapfileheader-> bfsize; dataoffset = bitmapfileheader-> bfoffbits; cout 

You will find that the new file is similar to the original file. The only difference is that there is a black horizontal line in the lower left corner of the new image. This is

for(int i=0;i<221;i++){data[i]=0;}

Cause... The order for storing BMP files is from left to right, from bottom to top. For more information, see the above link .. (Large-end method and small-end method, memory alignment ).
When I encountered a problem, I created an empty project and found that I was prompted by DWORD and word: an undeclared identifier. To solve this problem, you only need to include the header file that defines the data type .. You can also define the data type as follows:

typedef unsigned long       DWORD;
typedef unsigned short      WORD;

This is not the focus, but the reflection on the built-in and non-built-in data types of C ++, so you can see my next article (just an example ). If you know something more comprehensive, you may want to communicate with others ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.