将Mat图片加载到PictureControl控件中

来源:互联网 发布:淘宝模特快拍视频 编辑:程序博客网 时间:2024/04/30 04:22
//将Mat图片加载到PictureControl控件中,CWnd* pWnd为PictureControl的句柄
void CModelViewControllerDlg::showMatImgToWnd(CWnd* pWnd, const cv::Mat& img)

if(img.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 = -img.rows;   
bitMapinfo->bmiHeader.biWidth = img.cols;
bitMapinfo->bmiHeader.biBitCount= img.channels() *8;    
 
CRect drect;       
pWnd->GetClientRect(drect);    //pWnd指向CWnd类的一个指针 
CClientDC dc(pWnd);
HDC hDC =dc.GetSafeHdc();                  //HDC是Windows的一种数据类型,是设备描述句柄;
SetStretchBltMode(hDC, COLORONCOLOR);    
StretchDIBits(hDC,
0,
0,
drect.right,  //显示窗口宽度
drect.bottom,  //显示窗口高度
0,
0,
img.cols,     //图像宽度
img.rows,     //图像高度
img.data,   
bitMapinfo,   
DIB_RGB_COLORS, 
SRCCOPY);
0 0