void MyTestDialog::DrawPicToHDC(cv::Mat& img, UINT ID) { //清空 CStatic* pStatic = (CStatic*)GetDlgItem(ID); pStatic->SetBitmap(NULL); CRect rect; pStatic->GetClientRect(&rect); pStatic->GetDC()->FillSolidRect(rect.left ,rect.top ,rect.Width(),rect.Height(), RGB(240, 240, 240)); //// CDC* pDC = GetDlgItem(ID)->GetDC(); HDC hDC = pDC->GetSafeHdc(); //调整大小 //////// float widthRatio = (float)rect.Width() / img.cols; float heightRatio = (float)rect.Height() / img.rows; float resRatio = widthRatio < heightRatio ? widthRatio: heightRatio; int resWidth = img.cols * resRatio; int resHeight = img.rows * resRatio; cv::resize(img, img, cv::Size(resWidth, resHeight)); CRect drawRect; drawRect.SetRect(rect.TopLeft().x, rect.TopLeft().y, rect.TopLeft().x + img.cols - 1, rect.TopLeft().y + img.rows - 1); /////// IplImage* tmpimg = &img.operator IplImage(); CvvImage iimg; iimg.CopyOf(tmpimg); iimg.DrawToHDC(hDC, &drawRect); ReleaseDC(pDC); iimg.Destroy(); }
// CustomDialog 消息处理程序 void CustomDialog::DrawPicToHDC(IplImage *img, UINT ID) { CDC *pDC = GetDlgItem(ID)->GetDC(); HDC hDC= pDC->GetSafeHdc(); CRect rect; GetDlgItem(ID)->GetClientRect(&rect); CvvImage cimg; cimg.CopyOf(img); // 复制图片 cimg.DrawToHDC(hDC, &rect); // 将图片绘制到显示控件的指定区域内 cimg.Destroy(); ReleaseDC(pDC); }