opencv录制屏幕



void CMyseltestDlg::OnBnClickedTrans()
{
std::thread th(savevideo);
th.detach();
}
void savevideo()
{
ifwrite=true;
nWidth = GetSystemMetrics(SM_CXSCREEN);//得到屏幕的分辨率的x    
    nHeight = GetSystemMetrics(SM_CYSCREEN);//得到屏幕分辨率的y    
    hDDC = GetDC( GetDesktopWindow() );//得到屏幕的dc    
    hCDC = CreateCompatibleDC(hDDC);//    
  Mat img=gdiScreenCapture(); 
   VideoWriter vw;
   vw.open("d:\\2.avi",CV_FOURCC('M', 'P', '4', '2'),
  25,cvSize(img.cols,img.rows));
   while(ifwrite)
   { 
  img=gdiScreenCapture(); 
  waitKey(25);
  if(!img.data)
return;
  vw<   imshow("view",img);
   }
   vw.release();
}


void CMyseltestDlg::OnBnClickedstop()
{
ifwrite=false;

}
Mat gdiScreenCapture(){    
     hBitmap =CreateCompatibleBitmap(hDDC,nWidth,nHeight);//得到位图    
    SelectObject(hCDC,hBitmap); //好像总得这么写。             
    BitBlt(hCDC,0,0,nWidth,nHeight,hDDC,0,0,SRCCOPY);    
    Mat dst;
dst.create(cvSize(nWidth,nHeight),CV_8UC4);
GetBitmapBits(hBitmap,nWidth*nHeight*4,dst.data);
    cvtColor(dst,dst,CV_BGRA2BGR);    
return dst;        

你可能感兴趣的:(opencv,C++,局域网)