MFC截图

9 篇文章 0 订阅

/************************************************************************/
/*  返回一个实例的截屏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
	*/
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值