void Save8BitsBitmap
{
CFile file;
if(!file.Open(path, CFile::modeRead|CFile::modeWrite|CFile::modeCreate))
return FALSE;
BITMAP bp;
m_pPrim->GetBitmap(&bp); // m_pPrim 是要转成8位位图的原始图
// 构建BITMAPFILEHEADER结构
BITMAPFILEHEADER bfHeader;
int nWidthBytes = (bp.bmWidth + 3) / 4 * 4; // 保存每行是4个字节的整数倍
UINT size = bp.bmHeight * nWidthBytes;
bfHeader.bfType = MAKEWORD('B', 'M'); // BM
bfHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
bfHeader.bfSize = bfHeader.bfOffBits + size;
bfHeader.bfReserved1 = 0;
bfHeader.bfReserved2 = 0;
// 构建BITMAPINFOHEADER结构
BITMAPINFOHEADER biHeader;
biHeader.biSize = sizeof(BITMAPINFOHEADER);
biHeader.biWidth = bp.bmWidth;
biHeader.biHeight = bp.bmHeight;
biHeader.biPlanes = 1;
biHeader.biBitCount = 8;
biHeader.biCompression = BI_RGB;
biHeader.biSizeImage = size;
biHeader.biXPelsPerMeter= 0;
biHeader.biYPelsPerMeter= 0;
biHeader.biClrUsed = 0;
biHeader.biClrImportant = 0;
ExtractData();
// 创建调色板
RGBQUAD quad[256];
for(UINT i=0; i<256; i++)
{
quad[i].rgbBlue = quad[i].rgbGreen = quad[i].rgbRed = i;
quad[i].rgbReserved = 0;
}
PBYTE pData = new BYTE[size];
ZeroMemory(pData,size);
for(UINT h=0; h<bp.bmHeight; h++)
{
for(UINT w=0; w<bp.bmWidth; w++)
*(pData + h*nWidthBytes + w) = *(m_pData + (bp.bmHeight - h - 1)*bp.bmWidth + w);
}
// 位图写入文件
file.Write(&bfHeader, sizeof(bfHeader));
file.Write(&biHeader, sizeof(BITMAPINFOHEADER));
file.Write(quad, 256 * sizeof(RGBQUAD));
file.Write(pData, size);
delete pData;
pData = NULL;
file.Close();
}