Directly on the source code:
#include"stdafx.h"#include<iostream>#include<windows.h>using namespacestd;classcsavebytetobmp{ Public: BOOLSavedib2bmp (intFileNum,Constcstring& Bmpfilename,intIwidth,intIheight, BYTE *pbuffer); voidConstructbih (intNwidth,intNheight, bitmapinfoheader&BiH); voidCONTRUCTBHH (intNwidth,intnheight, bitmapfileheader& bhh);//add 2010-9-04;protected:Private:};
Then the CPP:
#include"stdafx.h"#include"SaveByteToBmp.h"//save buffer to BMP fileBOOLCsavebytetobmp::savedib2bmp (intFileNum,Constcstring& Bmpfilename,intIwidth,intIheight, BYTE *pbuffer) {Bitmapinfoheader BiH; Constructbih (iwidth, Iheight, BiH); Bitmapfileheader bhh; CONTRUCTBHH (iwidth, iheight, BHH); intWidthstep = (((iwidth * -) + to) & (~ to)) /8;//the actual size of each row occupied (each row is populated with a 4-byte boundary) intDibsize = Widthstep * iheight;//the size of the buffer (in bytes)CFile file; Try { if(file. Open (Bmpfilename, Cfile::modewrite |cfile::modecreate)) {//Write Filefile. Write ((LPSTR)&BHH,sizeof(Bitmapfileheader)); File. Write ((LPSTR)&bih,sizeof(Bitmapinfoheader)); File. Write (pbuffer, dibsize); File. Close (); return true; } } Catch (...) {MessageBox (NULL, _t ("csavebytetobmp::savedib2bmp"), _t ("Tips"), mb_iconerror); } return false;}//build BMP Bitmap File headervoidCSAVEBYTETOBMP::CONTRUCTBHH (intNwidth,intnheight, bitmapfileheader& bhh)//Add 2010-9-04{ intWidthstep = (((nwidth * -) + to) & (~ to)) /8;//the actual size of each row occupied (each row is populated with a 4-byte boundary)Bhh.bftype = ((WORD) ('M'<<8) |'B');//' BM 'Bhh.bfsize = (DWORD)sizeof(Bitmapfileheader) + (DWORD)sizeof(bitmapinfoheader) +widthstep *nheight; Bhh.bfreserved1=0; Bhh.bfreserved2=0; Bhh.bfoffbits= (DWORD)sizeof(Bitmapfileheader) + (DWORD)sizeof(Bitmapinfoheader);}//build BMP File Information headervoidCsavebytetobmp::constructbih (intNwidth,intNheight, bitmapinfoheader&BiH) { intWidthstep = (((nwidth * -) + to) & (~ to)) /8; Bih.bisize= +;//Header SizeBih.biwidth =nwidth; Bih.biheight=nheight; Bih.biplanes=1; Bih.bibitcount= -;//RGB encoded, bitBih.bicompression = Bi_rgb;//No compression non-compressionBih.bisizeimage = Widthstep*nheight *3; Bih.bixpelspermeter=0; Bih.biypelspermeter=0; Bih.biclrused=0; Bih.biclrimportant=0;}
Saves a byte array as a 24-bit BMP