/************************************************************************/
/* 返回一个实例的截屏HBITMAP */
/************************************************************************/
void PrintScreen::GetScreen2HBITMAP(HBITMAP &hBitmap )
{
//截屏图片参数
int image_width;
int image_height;
int image_depth;
方法1///
CDC *pDC;//屏幕DC
pDC = CDC::FromHandle(GetDC(NULL));//获取当前整个屏幕DC
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
image_width= pDC->GetDeviceCaps(HORZRES);
image_height = pDC->GetDeviceCaps(VERTRES);
CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDC);
//CBitmap memBitmap;
CBitmap *oldmemBitmap;//建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(pDC, image_width, image_height);
oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, image_width, image_height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC
hBitmap = (HBITMAP)memBitmap;
//以下代码保存memDC中的位图到文件
memDC.SelectObject(oldmemBitmap);
}
/************************************************************************/
/* 获取整个屏幕 */
/************************************************************************/
void PrintScreen::GetScreen(cv::Mat &screemImage)
{
//截屏图片参数
int image_width;
int image_height;
int image_depth;
int image_nchannels;
static int flag=0; //静态变量防止重复分配图片内存
//截屏函数,有两种方法
方法1///
CDC *pDC;//屏幕DC
pDC = CDC::FromHandle(GetDC(NULL));//获取当前整个屏幕DC
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
image_width= pDC->GetDeviceCaps(HORZRES);
image_height = pDC->GetDeviceCaps(VERTRES);
CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDC);
//CBitmap memBitmap, *oldmemBitmap;//建立和屏幕兼容的bitmap
CBitmap *oldmemBitmap;
if(flag==0)
{
memBitmap.CreateCompatibleBitmap(pDC, image_width, image_height); //同一个实例不能重复创建兼容位图,否则内存出错
}
oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, image_width, image_height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC
HBITMAP hBitmap =(HBITMAP) memBitmap.m_hObject;
//以下代码保存memDC中的位图到文件
BITMAP bmp;
memBitmap.GetBitmap(&bmp);//获得位图信息
int nChannels,depth;
BYTE *pBuffer;
image_nchannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8 ;
image_depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;
image_width=bmp.bmWidth;
image_height=bmp.bmHeight;
if(flag==0)
{
screemImage.create(cv::Size(image_width,image_height),CV_8UC4);
flag=1;
}
pBuffer = new BYTE[image_width*image_height*image_nchannels];
GetBitmapBits(hBitmap,image_height*image_width*image_nchannels,pBuffer);
memcpy(screemImage.data,pBuffer,image_height*image_width*image_nchannels);
delete pBuffer;
memDC.SelectObject(oldmemBitmap);
//方法1end
/*
/方法2//
int right=GetSystemMetrics(SM_CXSCREEN),left=0,top=0,bottom=GetSystemMetrics(SM_CYSCREEN);//定义截屏范围 此处设为全屏
int nWidth, nHeight;
HDC hSrcDC = NULL, hMemDC = NULL;
HBITMAP hBitmap = NULL, hOldBitmap = NULL;
hSrcDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
hMemDC = CreateCompatibleDC(hSrcDC);
nWidth = right - left;
nHeight = bottom - top;
hBitmap = CreateCompatibleBitmap(hSrcDC, nWidth, nHeight);
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
BitBlt(hMemDC, 0, 0, nWidth, nHeight, hSrcDC, left, top , SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
BITMAP bmp;
int nChannels,depth;
BYTE *pBuffer;
GetObject(hBitmap,sizeof(BITMAP),&bmp);
image_nchannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8 ;
image_depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;
image_width=bmp.bmWidth;
image_height=bmp.bmHeight;
if(flag==0)
{
//screemImage=cvCreateImage(cvSize(image_width,image_height),image_depth,image_nchannels);
screemImage.create(cv::Size(image_width,image_height),CV_8UC4);
flag=1;
}
pBuffer = new BYTE[image_width*image_height*image_nchannels];
GetBitmapBits(hBitmap,image_height*image_width*image_nchannels,pBuffer);
memcpy(screemImage.data,pBuffer,image_height*image_width*image_nchannels);
delete pBuffer;
SelectObject(hMemDC,hOldBitmap);
DeleteObject(hOldBitmap);
DeleteDC(hMemDC);
SelectObject(hSrcDC,hBitmap);
DeleteDC(hMemDC);
DeleteObject(hBitmap);
//方法2end
*/
}