一、在源文件dlg.cpp添加
#include "stdafx.h"
#include "afxdialogex.h"
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat image, imagedst;
int width,height;
二、在oninit里面添加
//页面样式
namedWindow("view", WINDOW_AUTOSIZE);
HWND hWnd = (HWND)cvGetWindowHandle("view");
HWND hParent = ::GetParent(hWnd);
::SetParent(hWnd, GetDlgItem(IDC_STATIC)->m_hWnd);
::ShowWindow(hParent, SW_HIDE);
三、在按钮方法下添加
CString picPath; //定义图片路径变量
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT, NULL, this); //选择文件对话框
if (dlg.DoModal() == IDOK)
{
picPath = dlg.GetPathName(); //获取图片路径
}
//CString to string 使用这个方法记得字符集选用“使用多字节字符”,不然会报错
string picpath = picPath.GetBuffer(0);
image = imread(picpath);
//Mat imagedst;
//以下操作获取图形控件尺寸并以此改变图片尺寸
CRect rect;
GetDlgItem(IDC_STATIC)->GetClientRect(&rect);
Rect dst(rect.left, rect.top, rect.right, rect.bottom);
width = rect.Width();
height = rect.Height();
resize(image, imagedst, cv::Size(rect.Width(), rect.Height()));
imshow("view", imagedst);