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
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);
}