一.截取整个屏幕:
CDC dc;
dc.CreateDC( " DISPLAY " ,NULL,NULL,NULL);
CBitmap bm;
CRect rect;
GetClientRect( & rect);
// int Width=GetSystemMetrics(SM_CXSCREEN);
// int Height=GetSystemMetrics(SM_CYSCREEN);
int Width = rect.Width();
int Height = rect.Height();
bm.CreateCompatibleBitmap( & dc,Width,Height);
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);
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(dc,bm, 0 ,bih.biHeight,lpData,(BITMAPINFO * ) & bih,DIB_RGB_COLORS);
// bm.GetBitmapBits(size,lpData); // 此函数在处理5-5-5模式的16位色下会出现颜色混乱
/**/ //
int nCount = 0 ;
static int filecount = 0 ;
CString name;
CString m_Path;
m_Path = " D:/MAP " ;
name.Format( " pict%04d.bmp " ,filecount ++ );
name = m_Path + name;
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(name,CFile::modeCreate | CFile::modeWrite))
... {
bf.WriteHuge(&bfh,sizeof(BITMAPFILEHEADER));
bf.WriteHuge(&bih,sizeof(BITMAPINFOHEADER));
bf.WriteHuge(lpData,size);
bf.Close();
nCount++;
}
// GlobalFreePtr(lpData);
GlobalFree(lpData);
/**/ /*if(nCount==1)
m_Number.Format("%d picture captured.",nCount);
else
m_Number.Format("%d pictures captured.",nCount);*/
dc.CreateDC( " DISPLAY " ,NULL,NULL,NULL);
CBitmap bm;
CRect rect;
GetClientRect( & rect);
// int Width=GetSystemMetrics(SM_CXSCREEN);
// int Height=GetSystemMetrics(SM_CYSCREEN);
int Width = rect.Width();
int Height = rect.Height();
bm.CreateCompatibleBitmap( & dc,Width,Height);
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);
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(dc,bm, 0 ,bih.biHeight,lpData,(BITMAPINFO * ) & bih,DIB_RGB_COLORS);
// bm.GetBitmapBits(size,lpData); // 此函数在处理5-5-5模式的16位色下会出现颜色混乱
/**/ //
int nCount = 0 ;
static int filecount = 0 ;
CString name;
CString m_Path;
m_Path = " D:/MAP " ;
name.Format( " pict%04d.bmp " ,filecount ++ );
name = m_Path + name;
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(name,CFile::modeCreate | CFile::modeWrite))
... {
bf.WriteHuge(&bfh,sizeof(BITMAPFILEHEADER));
bf.WriteHuge(&bih,sizeof(BITMAPINFOHEADER));
bf.WriteHuge(lpData,size);
bf.Close();
nCount++;
}
// GlobalFreePtr(lpData);
GlobalFree(lpData);
/**/ /*if(nCount==1)
m_Number.Format("%d picture captured.",nCount);
else
m_Number.Format("%d pictures captured.",nCount);*/
二.截取客户区图形:
BITMAPINFO bi;
//
信息头
void * pBits = NULL;
CRect rect; // 客户区窗口
GetClientRect( & rect);
int nWidth = rect.right;
int nHeight = rect.bottom;
// 填充信息头
ZeroMemory( & bi, sizeof (bi));
bi.bmiHeader.biSize = sizeof (bi.bmiHeader);
bi.bmiHeader.biHeight = nHeight;
bi.bmiHeader.biWidth = nWidth;
bi.bmiHeader.biPlanes = 1 ;
bi.bmiHeader.biBitCount = 24 ;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = 3 * nWidth * nHeight;
// 拷贝客户区至内存DC
// HDC hActiveDC=::GetDC(GetSafeHwnd()); // 参数
HDC hActiveDC = ::GetDC( this -> m_hWnd);
HDC hActiveWndCompatibleDC = CreateCompatibleDC(hActiveDC);
HBITMAP hActiveWndCompactibleBitmap = CreateCompatibleBitmap(hActiveDC,rect.right,rect.bottom);
SelectObject(hActiveWndCompatibleDC,hActiveWndCompactibleBitmap);
// 把hActiveDC句柄所指定的当前客户区图复制到hActiveWndCompatibleDC中
BitBlt(hActiveWndCompatibleDC, 0 , 0 ,rect.right,rect.bottom,hActiveDC, 0 , 0 ,SRCCOPY);
// 保存内存DC
HDC hBmpFileDC = CreateCompatibleDC(hActiveWndCompatibleDC);
// 创建一个位图可用作直接进行写操作
HBITMAP hBmpFileBitmap = CreateDIBSection(hActiveWndCompatibleDC, & bi,DIB_RGB_COLORS, & pBits,NULL, 0 );
SelectObject(hBmpFileDC,hBmpFileBitmap);
BitBlt(hBmpFileDC, 0 , 0 ,nWidth,nHeight,hActiveWndCompatibleDC, 0 , 0 ,SRCCOPY);
CString tempFileName; // 目标文件名
tempFileName = " D:/MAP.bmp " ;
HANDLE hFile = CreateFile(tempFileName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile != INVALID_HANDLE_VALUE)
... {
DWORD dwRet=0;
//填充文件头
BITMAPFILEHEADER bmfHeader;
ZeroMemory(&bmfHeader,sizeof(bmfHeader));
bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize=bi.bmiHeader.biSizeImage+bmfHeader.bfOffBits;
bmfHeader.bfType='MB';
WriteFile(hFile,&bmfHeader,sizeof(bmfHeader),&dwRet,NULL);
WriteFile(hFile,&bi.bmiHeader,sizeof(bi.bmiHeader),&dwRet,NULL);
WriteFile(hFile,pBits,bi.bmiHeader.biSizeImage,&dwRet,NULL);
CloseHandle(hFile);
}
void * pBits = NULL;
CRect rect; // 客户区窗口
GetClientRect( & rect);
int nWidth = rect.right;
int nHeight = rect.bottom;
// 填充信息头
ZeroMemory( & bi, sizeof (bi));
bi.bmiHeader.biSize = sizeof (bi.bmiHeader);
bi.bmiHeader.biHeight = nHeight;
bi.bmiHeader.biWidth = nWidth;
bi.bmiHeader.biPlanes = 1 ;
bi.bmiHeader.biBitCount = 24 ;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = 3 * nWidth * nHeight;
// 拷贝客户区至内存DC
// HDC hActiveDC=::GetDC(GetSafeHwnd()); // 参数
HDC hActiveDC = ::GetDC( this -> m_hWnd);
HDC hActiveWndCompatibleDC = CreateCompatibleDC(hActiveDC);
HBITMAP hActiveWndCompactibleBitmap = CreateCompatibleBitmap(hActiveDC,rect.right,rect.bottom);
SelectObject(hActiveWndCompatibleDC,hActiveWndCompactibleBitmap);
// 把hActiveDC句柄所指定的当前客户区图复制到hActiveWndCompatibleDC中
BitBlt(hActiveWndCompatibleDC, 0 , 0 ,rect.right,rect.bottom,hActiveDC, 0 , 0 ,SRCCOPY);
// 保存内存DC
HDC hBmpFileDC = CreateCompatibleDC(hActiveWndCompatibleDC);
// 创建一个位图可用作直接进行写操作
HBITMAP hBmpFileBitmap = CreateDIBSection(hActiveWndCompatibleDC, & bi,DIB_RGB_COLORS, & pBits,NULL, 0 );
SelectObject(hBmpFileDC,hBmpFileBitmap);
BitBlt(hBmpFileDC, 0 , 0 ,nWidth,nHeight,hActiveWndCompatibleDC, 0 , 0 ,SRCCOPY);
CString tempFileName; // 目标文件名
tempFileName = " D:/MAP.bmp " ;
HANDLE hFile = CreateFile(tempFileName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile != INVALID_HANDLE_VALUE)
... {
DWORD dwRet=0;
//填充文件头
BITMAPFILEHEADER bmfHeader;
ZeroMemory(&bmfHeader,sizeof(bmfHeader));
bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize=bi.bmiHeader.biSizeImage+bmfHeader.bfOffBits;
bmfHeader.bfType='MB';
WriteFile(hFile,&bmfHeader,sizeof(bmfHeader),&dwRet,NULL);
WriteFile(hFile,&bi.bmiHeader,sizeof(bi.bmiHeader),&dwRet,NULL);
WriteFile(hFile,pBits,bi.bmiHeader.biSizeImage,&dwRet,NULL);
CloseHandle(hFile);
}