void CPCDlg::WriteBmp(CBitmap *PBitmap, char m_FileName[])
{
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();
}
保存CBitmap到文件
最新推荐文章于 2022-04-12 17:17:31 发布