void CatchFullScreenToPic(const char *chFileName)
{
CDC *pDC = CDC::FromHandle(::GetDC(NULL)); //获取当前整个屏幕DC
int nBitPerPixel = pDC->GetDeviceCaps(BITSPIXEL); //获取颜色模式
int nWidth = pDC->GetDeviceCaps(HORZRES);
int nHeight = pDC->GetDeviceCaps(VERTRES);
CDC memDC; //内存DC
memDC.CreateCompatibleDC(pDC);
CBitmap memBitmap, *oldmemBitmap; //建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(pDC,nWidth,nHeight);
oldmemBitmap = memDC.SelectObject(&memBitmap); //将memBitmap选入内存DC
memDC.BitBlt(0,0,nWidth,nHeight,pDC,0,0,SRCCOPY); //复制屏幕图像到内存DC
BITMAP bmp;
memBitmap.GetBitmap(&bmp); //获取位图信息
FILE *pFile = fopen(chFileName,"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(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER); //到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight; //文件总大小
bfh.bfType = (WORD)0x4d42;
fwrite(&bfh,sizeof(BITMAPFILEHEADER),1,pFile); //写入位图文件头
fwrite(&bih,sizeof(BITMAPINFOHEADER),1,pFile); //写入位图信息头
byte *p = new byte[bmp.bmWidthBytes * bmp.bmHeight];
//获取位图数据
GetDIBits(memDC.m_hDC,(HBITMAP)memBitmap.m_hObject,0,nHeight,p,(LPBITMAPINFO)&bih,DIB_RGB_COLORS);
fwrite(p,bmp.bmWidthBytes * bmp.bmHeight,1,pFile);
delete []p;
fclose(pFile);
memDC.SelectObject(oldmemBitmap);
}
{
CDC *pDC = CDC::FromHandle(::GetDC(NULL)); //获取当前整个屏幕DC
int nBitPerPixel = pDC->GetDeviceCaps(BITSPIXEL); //获取颜色模式
int nWidth = pDC->GetDeviceCaps(HORZRES);
int nHeight = pDC->GetDeviceCaps(VERTRES);
CDC memDC; //内存DC
memDC.CreateCompatibleDC(pDC);
CBitmap memBitmap, *oldmemBitmap; //建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(pDC,nWidth,nHeight);
oldmemBitmap = memDC.SelectObject(&memBitmap); //将memBitmap选入内存DC
memDC.BitBlt(0,0,nWidth,nHeight,pDC,0,0,SRCCOPY); //复制屏幕图像到内存DC
BITMAP bmp;
memBitmap.GetBitmap(&bmp); //获取位图信息
FILE *pFile = fopen(chFileName,"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(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER); //到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight; //文件总大小
bfh.bfType = (WORD)0x4d42;
fwrite(&bfh,sizeof(BITMAPFILEHEADER),1,pFile); //写入位图文件头
fwrite(&bih,sizeof(BITMAPINFOHEADER),1,pFile); //写入位图信息头
byte *p = new byte[bmp.bmWidthBytes * bmp.bmHeight];
//获取位图数据
GetDIBits(memDC.m_hDC,(HBITMAP)memBitmap.m_hObject,0,nHeight,p,(LPBITMAPINFO)&bih,DIB_RGB_COLORS);
fwrite(p,bmp.bmWidthBytes * bmp.bmHeight,1,pFile);
delete []p;
fclose(pFile);
memDC.SelectObject(oldmemBitmap);
}