截取桌面并且BMP文件保存,出现了一点问题
下面是代码:
不知道哪里出了问题,请大家帮我找找
//定义图形大小 CRect re; GetDesktopWindow()->GetWindowRect(&re); int iWidth = re.Width() ; int iHeight = re.Height() ; int iPixel = 24; //图形格式参数 LPBITMAPINFO lpbmih = new BITMAPINFO; lpbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); lpbmih->bmiHeader.biWidth = iWidth; lpbmih->bmiHeader.biHeight = iHeight; lpbmih->bmiHeader.biPlanes = 1; lpbmih->bmiHeader.biBitCount = iPixel; lpbmih->bmiHeader.biCompression = BI_RGB; lpbmih->bmiHeader.biSizeImage = 0; lpbmih->bmiHeader.biXPelsPerMeter = 0; lpbmih->bmiHeader.biYPelsPerMeter = 0; lpbmih->bmiHeader.biClrUsed = 0; lpbmih->bmiHeader.biClrImportant = 0; //创建位图数据 HDC hdc,hdcMem; HBITMAP hBitMap = NULL; CBitmap *pBitMap = NULL; CDC *pMemDC = NULL; BYTE *pBits; // hdc = CreateIC(TEXT("DISPLAY"),NULL,NULL,NULL); hdc = GetDesktopWindow()->GetWindowDC()->m_hDC ; hdcMem = CreateCompatibleDC(hdc); hBitMap = CreateDIBSection(hdcMem,lpbmih,DIB_PAL_COLORS,(void **)&pBits,NULL,0); pBitMap = new CBitmap; pBitMap->Attach(hBitMap); pMemDC = new CDC; pMemDC->Attach(hdcMem); pMemDC->SelectObject(pBitMap); // CRect rc(0,0,iWidth,iHeight); pMemDC->SetBkMode(TRANSPARENT); //添加自绘图形 DrawCurve(pMemDC,rc); //保存到文件并创建位图结构 BITMAPFILEHEADER bmfh; ZeroMemory(&bmfh,sizeof(BITMAPFILEHEADER)); *((char *)&bmfh.bfType) = 'B'; *(((char *)&bmfh.bfType) + 1) = 'M'; bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); bmfh.bfSize = bmfh.bfOffBits + (iWidth * iHeight) * iPixel / 8; TCHAR szBMPFileName[128]; int iBMPBytes = iWidth * iHeight * iPixel / 8; strcpy(szBMPFileName,filename); CFile file; if(file.Open(szBMPFileName,CFile::modeWrite | CFile::modeCreate)) { file.Write(&bmfh,sizeof(BITMAPFILEHEADER)); file.Write(&(lpbmih->bmiHeader),sizeof(BITMAPINFOHEADER)); file.Write(pBits,iBMPBytes); file.Close(); } pMemDC->DeleteDC(); delete pMemDC; pMemDC = NULL; delete pBitMap; pBitMap = NULL; delete lpbmih; lpbmih = NULL;void CapScreen(char filename[]){ CDC *pDC; pDC = CDC::FromHandle(GetDC(GetDesktopWindow())); if(pDC == NULL) return; int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL); int Width = pDC->GetDeviceCaps(HORZRES); int Height = pDC->GetDeviceCaps(VERTRES); CDC memDC; if(memDC.CreateCompatibleDC(pDC) == 0) return; CBitmap memBitmap, *oldmemBitmap; if(memBitmap.CreateCompatibleBitmap(pDC, Width, Height) == NULL) return; oldmemBitmap = memDC.SelectObject(&memBitmap); if(oldmemBitmap == NULL) return; if(memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY) == 0) return; BITMAP bmp; memBitmap.GetBitmap(&bmp); FILE *fp = fopen(filename, "w+b"); BITMAPINFOHEADER bih = {0}; bih.biBitCount = bmp.bmBitsPixel; bih.biCompression = BI_RGB; bih.biHeight = bmp.bmHeight; bih.biPlanes = 1; bih.biSize = sizeof(BITMAPINFOHEADER); bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight; bih.biWidth = bmp.bmWidth; BITMAPFILEHEADER bfh = {0}; bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight; bfh.bfType = (WORD)0x4d42; fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp); fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp); byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight]; GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p, (LPBITMAPINFO) &bih, DIB_RGB_COLORS); fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp); delete [] p; fclose(fp); memDC.SelectObject(oldmemBitmap);}
[解决办法]
pMemDC = new CDC;
pMemDC->Attach(hdcMem);
#ifndef TEST
CBitmap *pbmpx=pMemDC->GetCurrentBitmap();
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_BITMAP,pbmpx->GetSafeHandle());
CloseClipboard();
#endif