void CGOMfcTemplate2Dlg::showImage(Mat src, UINT ID)
{
if (src.empty())
return;
CRect rect;
GetDlgItem(ID) ->GetClientRect( &rect ); // 在哪裏進行顯示?
CDC* pDC = GetDlgItem( ID ) ->GetDC();
HDC hDC = pDC ->GetSafeHdc(); // 獲取顯示控件(位置)的 HDC(設備句柄)
BITMAPINFO bmi = { 0 }; //生成bitmap
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biWidth = src.cols;
bmi.bmiHeader.biHeight = src.rows * -1;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
m_bitmapBits = new RGBTRIPLE[src.cols * src.rows];
Mat cv_bitmapBits(Size(src.cols, src.rows), CV_8UC3, m_bitmapBits);
src.copyTo(cv_bitmapBits); //拷貝到內存中
if (rect.Width() > src.cols)
{
SetStretchBltMode(
hDC, // handle to device context
HALFTONE);
}
else
{
SetStretchBltMode(
hDC, // handle to device context
COLORONCOLOR);
}
::StretchDIBits(hDC, 0, 0, rect.Width(), rect.Height(), 0, 0, src.cols,src.rows, m_bitmapBits,&bmi, DIB_RGB_COLORS, SRCCOPY);//顯示在街面上
ReleaseDC( pDC );
}