程序界面保存图片的问题
正常情况下,调用如下代码即可完成需求
CDC* pDC = GetDC(); CRect pRect; GetClientRect(&pRect); int w, h; w = pRect.right - pRect.left; h = pRect.bottom - pRect.top; CBitmap bm; bm.CreateCompatibleBitmap(pDC, w, h); CDC memdc; memdc.CreateCompatibleDC(pDC); CBitmap*pOld=memdc.SelectObject(&bm); memdc.BitBlt( 0, 0, w, h, pDC, pRect.left, pRect.top, SRCCOPY ); BITMAP btm; bm.GetBitmap(&btm); DWORD size=btm.bmWidthBytes*btm.bmHeight; LPSTR lpData=(LPSTR)::GlobalAlloc(GPTR,size); 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; GetDIBits(memdc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS); BITMAPFILEHEADER bfh; bfh.bfReserved1=bfh.bfReserved2=0; bfh.bfType=((WORD)('M'<< 8)|'B'); bfh.bfSize=54+size; bfh.bfOffBits=54; CFile bf; if(bf.Open(filePath,CFile::modeCreate|CFile::modeWrite)) { bf.Write(&bfh,sizeof(BITMAPFILEHEADER)); bf.Write(&bih,sizeof(BITMAPINFOHEADER)); bf.Write(lpData,size); bf.Close(); MessageBox(_T("保存成功!")); } else { MessageBox(_T("保存失败!")); } memdc.SelectObject( pOld ); bm.DeleteObject( ); memdc.DeleteDC( ); ::GlobalFree(lpData);