CClientDC dc(this); //客户区设备上下文
CBitmap bm;
CRect rect;
GetClientRect(rect);
int Width=rect.Width();
int Height=rect.Height();
bm.CreateCompatibleBitmap(&dc,Width,Height);
//Initializes a bitmap that is compatible with the device specified by dc
CDC tdc;
tdc.CreateCompatibleDC(&dc); //创建内存设备上下文
CBitmap*pOld=tdc.SelectObject(&bm);//内存设备上下文选中位图
tdc.BitBlt(0,0,Width,Height,&dc,0,0,SRCCOPY); //拷贝位图
tdc.SelectObject(pOld);
BITMAP btm;
bm.GetBitmap(&btm);
DWORD size=btm.bmWidthBytes*btm.bmHeight;
LPSTR lpData=(LPSTR)GlobalAllocPtr(GPTR,size);
//The BITMAPINFOHEADER structure contains information about the dimensions
//and color format of a device-independent bitmap (DIB)
BITMAPINFOHEADER bih;
bih.biBitCount=btm.bmBitsPixel;
bih.biClrImportant=0;
bih.biClrUsed=0;
bih.biCompression=0;
bih.biHeight=btm.bmHeight;
bih.biPlanes=1;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biSizeImage=size;
bih.biWidth=btm.bmWidth;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
//the bits of the specified bitmap and copies them into a buffer using the specified format.
GetDIBits(dc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
CString name="C://a.bmp";
//The BITMAPFILEHEADER structure contains information about the type,
//size, and layout of a file that contains a device-independent bitmap (DIB).
BITMAPFILEHEADER bfh;
bfh.bfReserved1=bfh.bfReserved2=0;
bfh.bfType=((WORD)('M'<< 8)|'B');//Specifies the file type. It must be MB(ox4D42)
bfh.bfSize=54+size;
bfh.bfOffBits=54;
CFile bf;
if(bf.Open(name,CFile::modeCreate|CFile::modeWrite)){
bf.WriteHuge(&bfh,sizeof(BITMAPFILEHEADER));
bf.WriteHuge(&bih,sizeof(BITMAPINFOHEADER));
bf.WriteHuge(lpData,size);
bf.Close();
}
GlobalFreePtr(lpData);