1 int nHeight = rc.Height(); 2 int nWidth = 0; 3 //截图宽度必须为4的倍数 4 if (rc.Width()%4) 5 { 6 nWidth = (rc.Width()/4+1)*4; 7 } 8 else 9 { 10 nWidth = rc.Width(); 11 } 12 *height = nHeight; 13 *width = nWidth; 14 15 HWND hWnd = ::GetDesktopWindow(); 16 HDC hdcSrc = ::GetDC(hWnd); 17 HDC hdcMem = ::CreateCompatibleDC(hdcSrc); 18 HBITMAP hBitmap = ::CreateCompatibleBitmap(hdcSrc, nWidth, nHeight); 19 HGDIOBJ hOldbmp = ::SelectObject(hdcMem, hBitmap); 20 CDC::FromHandle(hdcMem)->FillSolidRect(&rc, RGB(255, 255, 255)); 21 DrawPic(CDC::FromHandle(hdcMem), rc); 22 ::SelectObject(hdcMem, hOldbmp); 23 ::DeleteObject(hdcMem); 24 ::ReleaseDC(hWnd, hdcSrc); 25 26 //从bitmap提取RGB数据 27 if (hBitmap!= NULL) 28 { 29 CBitmap *bitmap = CBitmap::FromHandle(hBitmap); 30 BITMAP bmp; 31 bitmap->GetBitmap(&bmp); 32 int bitmapSize = bmp.bmWidthBytes*bmp.bmWidthBytes; 33 if (pRGBBuf==NULL) 34 { 35 return false; 36 } 37 DWORD dwCount = bitmap->GetBitmapBits(bitmapSize, pRGBBuf); 38 bitmap->DeleteObject(); 39 if (32 != m_nRGBBit) 40 { 41 //32位转24位 42 for (int nCount = 0; nCount < nWidth*nHeight*4;) 43 { 44 memmove(&pRGBBuf[nCount/4*3], &pRGBBuf[nCount], 3); 45 nCount += 4; 46 } 47 } 48 *nLen = nWidth*nHeight*m_nRGBBit/8; 49 } 50 51 //把图像数据倒置 52 int n = m_nRGBBit/8; 53 char tmp[2048*4]; 54 for(int i = 0; i < nHeight/2; i++) 55 { 56 memcpy(tmp, &(pRGBBuf[nWidth*i*n]), nWidth*n); 57 memcpy(&(pRGBBuf[nWidth*i*n]), &(pRGBBuf[nWidth*(nHeight-1-i)*n]), nWidth*n); 58 memcpy(&(pRGBBuf[nWidth*(nHeight-1-i)*n]), tmp, nWidth*n); 59 }
1 BOOL SaveRGBToFile(const char *pFileName, const char *pRGBBuf, int nLen, int nWidth, int nHeight) 2 { 3 FILE *file = fopen(pFileName, "wb+"); 4 if (NULL == file) 5 { 6 TRACE("open file failed, file name is:%s\n", pFileName); 7 return false; 8 } 9 //写文件头 10 BITMAPFILEHEADER fileheader; 11 ZeroMemory(&fileheader, sizeof(BITMAPFILEHEADER)); 12 memcpy((char *)&fileheader.bfType, "BM", 2); 13 fileheader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nWidth*nHeight*m_nRGBBit/8; 14 fileheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); 15 if (sizeof(BITMAPFILEHEADER) != fwrite((char *)&fileheader, sizeof(char), sizeof(BITMAPFILEHEADER), file)) 16 { 17 TRACE("write file error, file name is:%s\n", pFileName); 18 return false; 19 } 20 //写位图格式 21 BITMAPINFOHEADER infoHeader; 22 ZeroMemory(&infoHeader, sizeof(BITMAPINFOHEADER)); 23 infoHeader.biSize = sizeof(BITMAPINFOHEADER); 24 infoHeader.biSizeImage = nWidth*nHeight*m_nRGBBit/8; 25 infoHeader.biWidth = nWidth; 26 infoHeader.biHeight = nHeight; 27 infoHeader.biBitCount = m_nRGBBit; 28 if (sizeof(BITMAPINFOHEADER) != fwrite((char *)&infoHeader, sizeof(char), sizeof(BITMAPINFOHEADER), file)) 29 { 30 TRACE("write file error, file name is:%s\n", pFileName); 31 return false; 32 } 33 //写位图数据 34 if (nWidth*nHeight*m_nRGBBit/8 != fwrite(pRGBBuf, sizeof(char), nWidth*nHeight*m_nRGBBit/8, file)) 35 { 36 TRACE("write file error, file name is:%s\n", pFileName); 37 return false; 38 } 39 fclose(file); 40 return true; 41 }
实现在内存DC中绘图,然后对内存DC进行抓图并保存为bmp图片