【2012年终总结】之二 opencv + ds采集摄像头视频 GDI点点滴滴
在这个项目中用到了几个GDI的函数,包括CFont CPen CBrush等,一般要和设备上下文DC结合起来使用。
并且创建GDI对象使用完后一定要释放,否则可能会造成资源泄漏
对于CPen CFont CBrush用构造函数定义的GDI对象 和 用 CreateXXX获得的对象在释放时要调用DeleteObject
对于GetXXX获得的对象在释放时要使用ReleaseObject。
1 CDC *pDC0 = GetDlgItem(IDC_VIDEO0)->GetDC(); 2 CFont font; // GDI对象 3 font.CreatePointFont(200, _T("宋体")); //CreateXXX 4 CFont *oldFont = pDC0->SelectObject(&font); 5 pDC0->TextOut(20, 20, CString(_T("1"))); 6 pDC0->SelectObject(oldFont); 7 8 GetDlgItem(IDC_VIDEO0)->ReleaseDC(pDC0); 9 10 font.DeleteObject(); //释放GDI对象资源
下面的例子是画一个矩形的边框,矩形是一个Picture控件,当点击该控件时给这个控件的边缘画颜色
1 CRect rect; 2 CDC* pDC = GetDlgItem(m_nCurrentPicture)->GetDC(); 3 GetDlgItem(m_nCurrentPicture)->GetClientRect(&rect); 4 5 CPen pen(PS_SOLID, 10, RGB(0x99, 0x32, 0xcc)); 6 CPen *oldPen = NULL; 7 oldPen = pDC->SelectObject(&pen); 8 9 //上边框; 10 pDC->MoveTo(rect.TopLeft()); 11 pDC->LineTo(CPoint(rect.TopLeft().x + rect.Width(), //x 12 rect.TopLeft().y //y 13 ) ); 14 15 //右边框; 16 pDC->MoveTo(CPoint(rect.TopLeft().x + rect.Width(), //x 17 rect.TopLeft().y //y 18 )); 19 pDC->LineTo(rect.BottomRight()); 20 21 //下边框; 22 pDC->MoveTo(rect.BottomRight()); 23 pDC->LineTo(rect.TopLeft().x, //x 24 rect.BottomRight().y //y 25 ); 26 27 //左边框; 28 pDC->MoveTo(rect.TopLeft().x, //x 29 rect.BottomRight().y //y 30 ); 31 pDC->LineTo(rect.TopLeft()); 32 33 34 pDC->SelectObject(oldPen); 35 36 int ret = GetDlgItem(m_nCurrentPicture)->ReleaseDC(pDC); 37 pDC = NULL; 38 39 pen.DeleteObject();
多个Picture控件,每个Picture控件都在控件中间位置写上1 2 3 4等数字标识是哪个摄像头
1 void CCameraMonitorView::OnDraw(CDC* /*pDC*/) 2 { 3 // TODO: 在此添加专用代码和/或调用基类; 4 5 if (0 != m_nCurrentPicture) 6 { 7 ChoosePicture(m_nCurrentPicture); 8 } 9 10 int index = 0; 11 for (index = 0; index < MAX_CAMERAS_NUM; ++ index) 12 { 13 if (videoPicturesCtrl[index] != 0) 14 { 15 CDC *pDC = GetDlgItem(videoPicturesCtrl[index])->GetDC(); 16 CFont font; 17 font.CreatePointFont(800, _T("宋体")); 18 CFont *oldFont = pDC->SelectObject(&font); 19 pDC->SetBkMode(TRANSPARENT); 20 CRect rect; 21 GetDlgItem(videoPicturesCtrl[index])->GetClientRect(&rect); 22 23 int x = (rect.Width() / 2) - 20; 24 int y = (rect.Height() / 2) - 40; 25 char numArr[5] = {0}; 26 _itoa_s(index + 1, numArr, RADIX_10); 27 CString strDisplayNum(numArr); 28 29 pDC->TextOut(x, y, strDisplayNum); 30 pDC->SelectObject(oldFont); 31 32 GetDlgItem(IDC_VIDEO1)->ReleaseDC(pDC); 33 34 font.DeleteObject(); 35 36 } 37 } 38 39 }
从窗口指针获得DC
CDC *pDC = pWnd->GetDC();
根据DC获得HDC
HDC hDC = pDC->GetSafeHdc();
根据句柄获得窗口指针
CWnd *pWnd = FromHandle(hWnd);
void CJietu::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CDialog::OnPaint() HWND hWnd = m_hWnd; CWnd *pWnd = FromHandle(hWnd); CDC *pDC = pWnd->GetDC(); HDC hDC = pDC->GetSafeHdc(); StretchBlt(hDC, 0, 0, m_nCx, m_nCy, m_hDCGlobal, 0, 0, m_nCx, m_nCy, SRCCOPY); pWnd->ReleaseDC(pDC); }
另外一个例子
1 bool DrawPicToHDC(IplImage *img, HWND hWnd, bool bIsShowInfo) 2 { 3 CWnd *pWnd = CWnd::FromHandle(hWnd); 4 if (NULL == pWnd || FALSE == ::IsWindow(pWnd->m_hWnd)) 5 { 6 AfxMessageBox(_T("DrawPicToHDC error 2")); 7 return false; 8 } 9 10 CDC *pDC = pWnd->GetDC(); 11 HDC hDC = pDC->GetSafeHdc(); 12 CRect rect; 13 pWnd->GetClientRect(&rect); 14 CvvImage cimg; 15 cimg.CopyOf(img); 16 cimg.DrawToHDC(hDC, &rect); 17 18 pDC->SetBkMode(TRANSPARENT); //文字透明; 19 CTime time = CTime::GetCurrentTime(); //获得当前时间; 20 CString strTime; 21 strTime = time.Format("%Y-%m-%d %H:%M:%S"); 22 23 CFont font; 24 font.CreatePointFont(200, _T("宋体"), NULL); 25 CFont *oldFont = pDC->SelectObject(&font); 26 pDC->SetTextColor(RGB(255, 0, 0)); //文字颜色 27 28 pDC->TextOut(10, 10, strTime); 29 30 if (true == bIsShowInfo) 31 { 32 pDC->TextOut(10, 35, CString("正在录像...")); 33 } 34 35 pDC->SelectObject(oldFont); 36 37 ReleaseDC(hWnd,hDC); //释放DC 38 font.DeleteObject(); //释放GDI对象 39 40 return true; 41 }
**********************技术交流请 email:cuihao0532#163.com 欢迎转载,转载请注明出处!***************************** 如果对本文满意请扫描文章左侧【二维码添加微信】获取更多好玩、有趣、有益、有营养的料,
你我共同成长!Y(^_^)Y
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗