原理:ImlImage转成BitMAPINFO,然后调用StretchDIBits就可以了。
1.OnInitDialog()
m_img = cvLoadImage("test.jpg");
ASSERT(m_img != NULL);
memset(&m_bitmapinfoheader, 0, sizeof(m_bitmapinfoheader));
m_bitmapinfoheader.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_bitmapinfoheader.bmiHeader.biWidth = m_img->width;;
m_bitmapinfoheader.bmiHeader.biHeight = -m_img->height; // top-down image(什么图像顶行在这里的低行,具体看windows程序设计)
m_bitmapinfoheader.bmiHeader.biPlanes = 1;
m_bitmapinfoheader.bmiHeader.biBitCount = m_img->nChannels * 8;
m_bitmapinfoheader.bmiHeader.biCompression = BI_RGB;
m_bitmapinfoheader.bmiHeader.biSizeImage = 0;
img_height = m_bitmapinfoheader.bmiHeader.biHeight;
img_width = m_bitmapinfoheader.bmiHeader.biWidth;
lpBits = (BYTE*)m_img->imageData;
2.类成员定义
BITMAPINFO m_bitmapinfoheader;
BYTE *lpBits;
CRect m_rc_show;
int img_width, img_height;
3.对话框OnPaint函数
CPaintDC dc(this);
GetDlgItem(IDC_STATIC_IMG)->GetWindowRect(m_rc_show);
ScreenToClient(m_rc_show);
//ClientToScreen(&rc);
int bRet = StretchDIBits(
dc.GetSafeHdc(),
m_rc_show.left,
m_rc_show.top,
m_rc_show.Width(),
m_rc_show.Height(),
0,
0,
img_width,
abs(img_height),
lpBits,
(BITMAPINFO*)&m_bitmapinfoheader,
DIB_RGB_COLORS, SRCCOPY
);
F5,查看结果,好像图片较大会被拉伸。有了这个方法就可以处理opencv 采集usb 摄像头数据,并且处理了。