首页 > 代码库 > C++保存图像数据至BMP文件

C++保存图像数据至BMP文件

BOOL savebitmap(const BITMAPINFOHEADER* pHeader, const char* data, int nImgW, int nImgH, const char* filename){    BITMAPINFOHEADER bmiHdr; //定义信息头                bmiHdr.biSize = sizeof(BITMAPINFOHEADER);                  bmiHdr.biWidth = nImgW;           bmiHdr.biHeight = nImgH;           bmiHdr.biPlanes = 1;           bmiHdr.biBitCount = 24;           bmiHdr.biCompression = BI_RGB;           bmiHdr.biSizeImage = nImgW*nImgH*3;         bmiHdr.biXPelsPerMeter = 0;           bmiHdr.biYPelsPerMeter = 0;          bmiHdr.biClrUsed = 0;        bmiHdr.biClrImportant = 0;         FILE* fp = fopen(filename, "wb");    if (fp)    {        BITMAPFILEHEADER fheader = { 0 };        fheader.bfType = M << 8 | B;        fheader.bfSize = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) + pHeader->biSizeImage;        fheader.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);        fwrite(&fheader, 1, sizeof(fheader), fp);        fwrite(pHeader, 1, sizeof(BITMAPINFOHEADER), fp);        fwrite(data, 1, bmiHdr.biSizeImage, fp);        fclose(fp);                return TRUE;    }    else      return FALSE;}