首先给mfc图片控件添加一个变量m_picture
然后在按键单机处理函数中添加代码:
void CxxxDlg::OnBnClickedButton_xxx()//按钮_打开图片
{
//TODO: 在此添加控件通知处理程序代码
//打开文件管理器
CString strFile = _T("");
CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.jpg)|*.jpg|All Files (*.*)|*.*||"), NULL);
if (dlgFile.DoModal())
{
strFile = dlgFile.GetPathName();//获取打开的图片路径
}
int flag=strFile.GetLength();//判断是否打开了图像
if (flag>0)//如果打开了图像则显示
{
//opencv清除上一张图
cvDestroyAllWindows();
//mfc清除上一张图
m_picture.SetBitmap(NULL);
this->RedrawWindow();
//创建OpenCV窗口
namedWindow("ImageShow");
//嵌套opencv窗口
HWND hWnd2 = (HWND)cvGetWindowHandle("ImageShow");
HWND hParent = ::GetParent(hWnd2);
::SetParent(hWnd2, GetDlgItem(IDC_STATIC1)->m_hWnd);
::ShowWindow(hParent, SW_HIDE);
//CString转cv::String
USES_CONVERSION;
String img_path(W2A(strFile));
Mat mat = imread(img_path);//opencv读取图片
imshow("ImageShow", mat);//opencv显示图片
waitKey(1);
}
}