MFC下的OpenCV截图与API函数截图

1、使用OpenCV的函数截图,以下是一位网友写的http://blog.csdn.net/chenli2010/article/details/7899773

     源代码如下:

#include<opencv/cv.h>
#include<opencv/highgui.h>
#include<iostream>

CvPoint pt1 = cvPoint(0,0);
CvPoint pt2 = cvPoint(0,0);
bool is_selecting = false;

// callback function
void cvMouseCallback(int mouseEvent,int x,int y,int flags,void* param)
{
 switch(mouseEvent)
 {
 case CV_EVENT_LBUTTONDOWN:
  pt1 = cvPoint(x,y);
  pt2 = cvPoint(x,y);
  is_selecting = true;
  break;
 case CV_EVENT_MOUSEMOVE:
  if(is_selecting)
   pt2 = cvPoint(x,y);
  break;
 case CV_EVENT_LBUTTONUP:
  pt2 = cvPoint(x,y);
  is_selecting = false;
  break;
 }
 return;
}

int main(int argc,char* argv[])
{
 char img_path[80] = "0.jpg";
 char save_path[80] = "save.jpg";
 char* window = "img";

 IplImage* img = cvLoadImage(img_path); 
 IplImage* img_show = cvCloneImage(img);

 cvNamedWindow(window,CV_WINDOW_AUTOSIZE);
 cvSetMouseCallback(window,cvMouseCallback);

 bool shift_on = false;

 char text[80];
 CvFont font;
 cvInitFont(&font,CV_FONT_HERSHEY_PLAIN,1.0,1.0);
 while(true)
 {
  cvCopy(img,img_show);
  cvRectangle(img_show,pt1,pt2,cvScalar(255,255,255));
  sprintf(text,"roi = cvRect(%d,%d,%d,%d)",pt1.x,pt1.y,std::abs(pt2.x-pt1.x),std::abs(pt2.y-pt1.y));
  cvPutText(img_show,text,cvPoint(10,20),&font,cvScalar(0,0,255));

  cvShowImage(window,img_show);
  char key = cvWaitKey(10);
  switch(key)
  {
  case '\t':
   shift_on = !shift_on; break;

  case 'a':
   pt1.x--; pt2.x--; break;
  case 's':
   pt1.y++; pt2.y++; break;
  case 'd':
   pt1.x++; pt2.x++; break;
  case 'w':
   pt1.y--; pt2.y--; break;

  case '1':
   if(shift_on) pt1.x--;
   else pt2.x--;
   break;
  case '2':
   if(shift_on) pt2.y++;
   else pt1.y++;
   break;
  case '3':
   if(shift_on) pt2.x++;
   else pt1.x++;
   break;
  case '5':
   if(shift_on) pt1.y--;
   else pt2.y--;
   break;

  case '\r':
   cvSetImageROI(img,cvRect(pt1.x,pt1.y,std::abs(pt2.x-pt1.x),std::abs(pt2.y-pt1.y)));
   cvSaveImage(save_path,img);
   cvResetImageROI(img);
   break;
  };

  if(key==27) break;
 }

 cvReleaseImage(&img);
 cvReleaseImage(&img_show);
 return 0;
}

 

2、使用系统的API函数

以下摘自http://hi.baidu.com/winkyao/blog/item/db7e3fd07540023e06088b29.html

先在CCupWindowDlg的头文件中定义一个私有变量CDC CompatibleDC;
void CCupWindowDlg::OnButton1()
{
 // TODO: Add your control notification handler code here
 bCapture=TRUE;
 
 int cx=GetSystemMetrics(SM_CXSCREEN);                   //这里重新设置了对话框的大小,置顶对话框,而且去除了VC的边框
 int cy=GetSystemMetrics(SM_CYSCREEN);
    //保存原来窗口的大小,以便恢复
 GetClientRect(&rectOld);
 CDC   dc;
    dc.CreateDC("DISPLAY",NULL,NULL,NULL);
 CBitmap   bitmap;                 //创建一个位图,得到了这个位图句柄,也就是API当中的HBITMAP,一切就好办了
 bitmap.CreateCompatibleBitmap(&dc,cx,cy);    //为其创建一个兼容的位图
    if(!CompatibleDC.m_hDC )
 {
  CompatibleDC.CreateCompatibleDC(&dc);
 }
    CBitmap *pOld=CompatibleDC.SelectObject(&bitmap);   //兼容DC在初始化时是以单色位图初始化的,把位图句柄选进兼容DC,扩展其显示面大小。
 CompatibleDC.StretchBlt(0,0,cx,cy,&dc,0,0,cx,cy,SRCCOPY);
     //这里,把整个屏幕的图,贴到了内存设备描述表  
    ModifyStyle(WS_BORDER,0,0);                //要在获取到了屏幕位图之后,再置顶窗口,不然会截取失败
    this->SetWindowPos(&wndTopMost,0,0,cx,cy,SWP_SHOWWINDOW);
    CompatibleDC.SelectObject(pOld);
    //接下来当然是改变对话框的设备描述表了。响应WM_ERASEBKGND消息
 
}

 BOOL CCupWindowDlg::OnEraseBkgnd(CDC* pDC)
{
 // TODO: Add your message handler code here and/or call default
    if(bCapture)
 {
  CBitmap bitmap;
  CRect rect;
  GetClientRect(&rect);
  pDC->BitBlt(0,0,rect.Width(),rect.Height(),&CompatibleDC,0,0,SRCCOPY);
  return TRUE;
 }
 else
  return CDialog::OnEraseBkgnd(pDC);
}

void CCupWindowDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 if(!bClicking&& bCapture)           //已经按了截图键
 {
  bClicking=TRUE;
        ptBeg=point;
  ptEnd=ptBeg;
 }
 

 
 CDialog::OnLButtonDown(nFlags, point);
}

void CCupWindowDlg::OnMouseMove(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 if(bClicking && bCapture)
 {
  HDC hdc;
  hdc=::GetDC(m_hWnd);
        DrawBlock(hdc,ptBeg,ptEnd);
  ptEnd=point;
  DrawBlock(hdc,ptBeg,ptEnd);
  ::ReleaseDC(m_hWnd,hdc);
 }

 CDialog::OnMouseMove(nFlags, point);
}

//按下右键,可以撤销

void CCupWindowDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 if(bCapture)
 {
       bCapture=FALSE;
    ::SetWindowPos(m_hWnd,HWND_NOTOPMOST,0,0,rectOld.right-rectOld.left,rectOld.bottom-rectOld.top,SWP_SHOWWINDOW);
 }
 CDialog::OnRButtonDown(nFlags, point);
}

void CCupWindowDlg::DrawBlock(HDC hdc,POINT ptBeg,POINT ptEnd)
{
    SetROP2(hdc,R2_NOT);   //如果已经画了,那么就擦除
 SelectObject(hdc,GetStockObject(NULL_BRUSH));
    Rectangle(hdc,ptBeg.x,ptBeg.y,ptEnd.x,ptEnd.y);
}

 

 

你可能感兴趣的:(api,null,mfc,Path,callback,border)