FILE *fp; // 定义一个文件指针
BMP_HEADER bmpHeader; // 定义一个 BMP 文件头的结构体
BMP_INFO bmpInfo; // 定义一个 BMP 文件信息结构体 if((fp = fopen(BMPFile.c_str(), "wb")) == NULL){
MSG("Cann't open the file!","Error",MB_OK);
return -1;
} bmpInfo.bSize = 40; //固定
bmpInfo.bWidth = Width;
bmpInfo.bHeight = Height;
bmpInfo.bBitCount = BitCount; //计算四字节对齐的宽度*高度
long ImageSize4bytes;
ImageSize4bytes = bmpInfo.bWidth*sizeof(BMP_POINT); if(ImageSize4bytes % 4!=0){ImageSize4bytes += 4-(ImageSize4bytes % 4);}
ImageSize4bytes *= bmpInfo.bHeight;

bmpInfo.bImageSize = ImageSize4bytes;
bmpInfo.bCompression = 0;
bmpInfo.bClrImportant = 0;
bmpInfo.bClrUsed = 0;
bmpInfo.bXPelsPerMeter = 0;
bmpInfo.bYPelsPerMeter = 0;
bmpInfo.bPlanes = 1; // 调色板数量 bmpHeader.bType = 0x4D42; //BM ascii码
bmpHeader.bOffset = 54;
bmpHeader.bSize = bmpInfo.bImageSize + bmpHeader.bOffset;
bmpHeader.bReserved1 = 0;
bmpHeader.bReserved2 = 0; //让 fp 指向 bmp 文件的开始
//第 2 个参数是偏移量 第三个参数是文件起始地址 所以此函数执行成功后文件指
fseek(fp, 0, SEEK_SET);
// 写入文件
fwrite(&bmpHeader, sizeof(bmpHeader), 1, fp);
fwrite(&bmpInfo, sizeof(bmpInfo), 1, fp);
fwrite(pData, 1, bmpInfo.bImageSize, fp); fclose(fp);