MFC Picture Control显示OpenCV图像

MFC Picture Control显示OpenCV图像

flyfish

方法1

IDC_STATIC_IMAGE是picture control的ID

加载图像函数的实现

//s 表示路径
        cv::Mat m_dst;
        cv::Mat srcImage = cv::imread(s);
        CRect rect;
        CStatic* pWnd = (CStatic*)GetDlgItem(IDC_STATIC_IMAGE);

        pWnd ->GetClientRect(&rect);
        cv::resize(srcImage, m_dst, cv::Size(rect.Width(), rect.Height()));

        cv::flip(m_dst, m_dst, 0);//因为是反着的图像,是以左下角为原点的显示,窗口显示的图像原点是左上角,相当于是X轴翻
转



            BITMAPINFO bitInfo;
        bitInfo.bmiHeader.biBitCount = 24;
        bitInfo.bmiHeader.biWidth = rect.Width();
        bitInfo.bmiHeader.biHeight = rect.Height();
        bitInfo.bmiHeader.biPlanes = 1;
        bitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        bitInfo.bmiHeader.biCompression = BI_RGB;
        bitInfo.bmiHeader.biClrImportant = 0;
        bitInfo.bmiHeader.biClrUsed = 0;
        bitInfo.bmiHeader.biSizeImage = 0;
        bitInfo.bmiHeader.biXPelsPerMeter = 0;
        bitInfo.bmiHeader.biYPelsPerMeter = 0;


        CDC * pDc = pWnd ->GetWindowDC();

        StretchDIBits(pDc->m_hDC, 0, 0,
            rect.Width(), rect.Height(), 0, 0,
            rect.Width(), rect.Height(),
            m_dst.data, &bitInfo, DIB_RGB_COLORS, SRCCOPY);

ReleaseDC(pDc);

方法2
存储到文件之后 直接加载

CStatic* pWnd = (CStatic*)GetDlgItem(IDC_STATIC_IMAGE);

        CImage img;     
        CRect rect;                     
        img.Load(L"C:\\1.jpg");     
        pWnd ->GetClientRect(&rect);  
        CDC * pDc = pWnd ->GetWindowDC();   
        img.Draw(pDc->m_hDC, imageRect);    
        ReleaseDC(pDc);

方法3
使用OpenCV 写入文件,然后使用CImage加载

Picture Control的ID 是IDC_STATIC_IMAGE
头文件声明
CImage* m_pImage;

实现文件
OnInitDialog() 
m_pImage = new CImage;




//加载
CString localfile = L"C:\\test.png"; 

        m_pImage->Destroy();
        m_pImage->Load(localfile);
        CRect imageRect;
        CStatic* m_pWnd = (CStatic*)GetDlgItem(IDC_STATIC_IMAGE);

        CDC *pDc = m_pWnd->GetDC();

        m_pWnd->GetClientRect(imageRect);
        pDc->SetStretchBltMode(COLORONCOLOR);
        m_pImage->Draw(pDc->m_hDC, imageRect);
        ReleaseDC(pDc);


//加载之后需要处理OnPaint
OnPaint()
{
    CPaintDC dc(this); // device context for painting
    if (!m_pImage->IsNull())
    {
        CRect imageRect;
        CStatic* m_pWnd = (CStatic*)GetDlgItem(IDC_STATIC_IMAGE);
        m_pWnd->GetClientRect(imageRect);
        CDC *pDc = m_pWnd->GetDC();
        pDc->SetStretchBltMode(COLORONCOLOR);
        m_pImage->Draw(pDc->m_hDC, imageRect);
        ReleaseDC(pDc);
    }
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/flyfish1986/article/details/53493867
文章标签: opencv
个人分类: VC++ MFC
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭