Dlg.h头文件中添加函数声明
class C枪支编码识别Dlg : public CDialogEx
{
// 构造
public:
C枪支编码识别Dlg(CWnd* pParent = NULL); // 标准构造函数
// 对话框数据
enum { IDD = IDD_MY_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
void C枪支编码识别Dlg::showMatImgToWnd(CWnd* pictureWnd, const Mat& disimg); //函数声明
Dlg.cpp中添加下面的函数
(第一个参数为picture control控件句柄,后一个参数为要显示的Mat图像)
void C枪支编码识别Dlg::showMatImgToWnd(CWnd* pictureWnd, const Mat& disimg)//Mat图像加载到picture control函数
{
if (disimg.empty()) return ;
static BITMAPINFO *bitMapinfo = NULL;
static bool First = TRUE;
if (First)
{
BYTE *bitBuffer = new BYTE[40 + 4 * 256];//开辟一个内存区域
if (bitBuffer == NULL)
{
return;
}
First = FALSE;
memset(bitBuffer, 0, 40 + 4 * 256);
bitMapinfo = (BITMAPINFO *)bitBuffer;
bitMapinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitMapinfo->bmiHeader.biPlanes = 1;
for (int i = 0; i<256; i++)
{ //颜色的取值范围 (0-255)
bitMapinfo->bmiColors[i].rgbBlue = bitMapinfo->bmiColors[i].rgbGreen = bitMapinfo->bmiColors[i].rgbRed = (BYTE)i;
}
}
bitMapinfo->bmiHeader.biHeight = -disimg.rows;
bitMapinfo->bmiHeader.biWidth = disimg.cols;
bitMapinfo->bmiHeader.biBitCount = disimg.channels() * 8;
CRect drect;
pictureWnd->GetClientRect(drect); //pWnd指向CWnd类的一个指针
CClientDC dc(pictureWnd);
HDC hDC = dc.GetSafeHdc(); //HDC是Windows的一种数据类型,是设备描述句柄;
SetStretchBltMode(hDC, COLORONCOLOR);
StretchDIBits(hDC,
0,
0,
drect.right, //显示窗口宽度
drect.bottom, //显示窗口高度
0,
0,
disimg.cols, //图像宽度
disimg.rows, //图像高度
disimg.data,
bitMapinfo,
DIB_RGB_COLORS,
SRCCOPY);
}
获取picture control控件句柄
CWnd *hwnd = GetDlgItem(IDC_STATIC);