首先,这个函数只能针对24色的bitmap.
void CAppDlg::WriteBmp(CBitmap *PBitmap)
{
BITMAPINFO *m_pBMI;
BYTE *m_pDIBData;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bi;
BITMAP BM;
PBitmap->GetObject(sizeof(BM),&BM);
CClientDC dc(this);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = BM.bmWidth;
bi.biHeight = BM.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = 0;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
bfh.bfType=0x4d42;
bfh.bfOffBits=sizeof(bfh)+sizeof(BITMAPINFOHEADER);
bfh.bfSize=bfh.bfOffBits+BM.bmWidth*BM.bmHeight*3;
bfh.bfReserved1=0;
bfh.bfReserved2=0;
m_pBMI=(BITMAPINFO*)new char[sizeof(BITMAPINFO)];
m_pDIBData=(BYTE*)new char[bfh.bfSize-bfh.bfOffBits];
memcpy(m_pBMI,&bi,sizeof(BITMAPINFOHEADER));
GetDIBits(dc.GetSafeHdc(), (HBITMAP)PBitmap->GetSafeHandle(), 0l, (DWORD)bi.biHeight,
(LPVOID)m_pDIBData,(LPBITMAPINFO)m_pBMI, (DWORD)DIB_RGB_COLORS);
CFile file;
if(!file.Open(m_FileName,CFile::modeCreate|CFile::modeWrite))
{
MessageBox("error!");
return;
}
file.Write(&bfh,sizeof(bfh));
file.Write(m_pBMI,sizeof(BITMAPINFOHEADER));
file.Write(m_pDIBData,BM.bmWidth*BM.bmHeight*3);
file.Close();
delete m_pBMI;
delete m_pDIBData;
}
其实横简单吧!在本站的GetPicture软件中有源代码.