【转自http://blog.csdn.net/dcrmg/article/details/51913160】

以下是Key code:

1. 在OnInitDialog初始化函数里添加代码

[cpp] view plain copy
  1.        namedWindow("view",WINDOW_AUTOSIZE);  
  2. HWND hWnd = (HWND) cvGetWindowHandle("view");  
  3. HWND hParent = ::GetParent(hWnd);  
  4. ::SetParent(hWnd, GetDlgItem(IDC_STATIC_Pic)->m_hWnd);  
  5. ::ShowWindow(hParent, SW_HIDE);  
其中IDC_STATIC_Pic是Picture Control图形控件的ID;

2. 定义打开图片按钮事件

[cpp] view plain copy
  1.        CString picPath;   //定义图片路径变量  
  2. CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,   
  3.     NULL, this);   //选择文件对话框  
  4. if(dlg.DoModal() == IDOK)  
  5. {  
  6.     picPath= dlg.GetPathName();  //获取图片路径  
  7. }  
  8. //CString to string  使用这个方法记得字符集选用“使用多字节字符”,不然会报错  
  9. string picpath=picPath.GetBuffer(0);    
  10.   
  11. Mat image=imread(picpath);     
  12. Mat imagedst;  
  13. //以下操作获取图形控件尺寸并以此改变图片尺寸  
  14. CRect rect;  
  15. GetDlgItem(IDC_STATIC_Pic)->GetClientRect(&rect);  
  16. Rect dst(rect.left,rect.top,rect.right,rect.bottom);  
  17. resize(image,imagedst,cv::Size(rect.Width(),rect.Height()));   
  18. imshow("view",imagedst);      

适应图形控件的显示效果:

MFC 显示Mat,不使用Mat到CImage转换

注: Picture Control控件类型不需要必须是矩形(Rectangle),使用默认的类型帧(Frame)也可以正常显示。





相关文章: