保存CBitmap为bmp图片

保存CBitmap为bmp图片
2008-10-20 18:06
void CMyStatic::SaveBmpFile(CString str,CBitmap& bitmap,CDC* pDC)
{
//open & create bitmap file
CFile file;
file.Open(str,CFile::modeCreate|CFile::modeWrite);
//create bitmap objects
BITMAP bitm;
bitmap.GetBitmap(&bitm);
char* pBuffer;
int ncharsize=bitm.bmHeight*bitm.bmWidthBytes;
pBuffer=new char[ncharsize];
memset(pBuffer,0xFF,ncharsize);
BITMAPINFOHEADER fileinfo;
BITMAPINFO bmpinfo;
bmpinfo.bmiHeader.biSize=40;
bmpinfo.bmiHeader.biWidth=bitm.bmWidth;
bmpinfo.bmiHeader.biHeight=bitm.bmHeight;
bmpinfo.bmiHeader.biPlanes=bitm.bmPlanes;
bmpinfo.bmiHeader.biBitCount=bitm.bmBitsPixel;
bmpinfo.bmiHeader.biCompression=BI_RGB;
int nline=GetDIBits(GetDC()->GetSafeHdc(),
(HBITMAP)bitmap.GetSafeHandle(),0,bmpinfo.bmiHeader.biHeight,pBuffer,&bmpinfo,DIB_RGB_COLORS);
int nsize=nline*bitm.bmWidthBytes;
char cc;
cc=66;
file.Write(&cc,1);
cc=77;
file.Write(&cc,1);
long size=54+nsize;
file.Write(&size,4);
size=0;
file.Write(&size,4);
size=54;
file.Write(&size,4);
fileinfo.biSize=40;
fileinfo.biWidth=bitm.bmWidth;
fileinfo.biHeight=bitm.bmHeight;
fileinfo.biPlanes=bitm.bmPlanes;
fileinfo.biBitCount=bitm.bmBitsPixel;
fileinfo.biCompression=BI_RGB;
fileinfo.biSizeImage=0;
fileinfo.biXPelsPerMeter=0;
fileinfo.biYPelsPerMeter=0;
fileinfo.biClrUsed=0;
fileinfo.biClrImportant=0;
file.Write(&fileinfo,fileinfo.biSize);
file.Write(pBuffer,nsize);
file.Close();
delete pBuffer;
}

你可能感兴趣的:(保存CBitmap为bmp图片)