将Mat图像显示到MFC PictureControl控件上

2019-04-15 18:19发布

void CCameraTimeDlg::ShowMatImage(Mat& src) { try { if (src.empty())//没有图像时推出 { return; } BITMAPINFO *pBmpInfo1; if(src.channels()== 1)//灰度图像显示 { pBmpInfo1=(BITMAPINFO *)new char[sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)];////bmp图信息头 pBmpInfo1->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pBmpInfo1->bmiHeader.biWidth = src.cols;//src_rec.Width(); pBmpInfo1->bmiHeader.biHeight =-src.rows; //-src_rec.Height();//为负值 pBmpInfo1->bmiHeader.biPlanes = 1; pBmpInfo1->bmiHeader.biBitCount = 8;//八位 pBmpInfo1->bmiHeader.biCompression = BI_RGB; pBmpInfo1->bmiHeader.biSizeImage = 0; pBmpInfo1->bmiHeader.biXPelsPerMeter = 0; pBmpInfo1->bmiHeader.biYPelsPerMeter = 0; pBmpInfo1->bmiHeader.biClrUsed = 0; pBmpInfo1->bmiHeader.biClrImportant = 0; for(int i = 0;i<256;i++)//只有灰度图像需要颜 {MOD}表 { pBmpInfo1->bmiColors[i].rgbBlue = pBmpInfo1->bmiColors[i].rgbGreen = pBmpInfo1->bmiColors[i].rgbRed = (BYTE)i; pBmpInfo1->bmiColors[i].rgbReserved = 0; } } if(src.channels()== 3) { pBmpInfo1=(BITMAPINFO*) new char[sizeof(BITMAPINFOHEADER)]; pBmpInfo1->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pBmpInfo1->bmiHeader.biWidth = src.cols;//src_rec.Width(); pBmpInfo1->bmiHeader.biHeight =-src.rows; //-src_rec.Height();//为负值 pBmpInfo1->bmiHeader.biPlanes = 1; pBmpInfo1->bmiHeader.biBitCount = 24;//24位 pBmpInfo1->bmiHeader.biCompression = BI_RGB; pBmpInfo1->bmiHeader.biSizeImage = 0; pBmpInfo1->bmiHeader.biXPelsPerMeter = 0; pBmpInfo1->bmiHeader.biYPelsPerMeter = 0; pBmpInfo1->bmiHeader.biClrUsed = 0; pBmpInfo1->bmiHeader.biClrImportant = 0; } HDC h_dc=GetDlgItem(IDC_PIC_Corrected)->GetDC()->GetSafeHdc(); CRect BoxSize; GetDlgItem(IDC_PIC_Corrected)->GetClientRect(&BoxSize); SetStretchBltMode( h_dc, // handle to device context HALFTONE ); int jj=StretchDIBits(h_dc,BoxSize.left,BoxSize.top,BoxSize.Width(),BoxSize.Height(),0,0,src.cols,src.rows,(void *)src.data, (BITMAPINFO*)pBmpInfo1,DIB_RGB_COLORS,SRCCOPY); delete []pBmpInfo1; return; } catch (...) { return; } }

欢迎扫码关注我的微信公众号

在这里插入图片描述