因为CvvImage的Show或DrawToHdc方法都只能临时将图片显于控件之上,如果窗口大小发生变化或发生重绘,则原来的图会被抹去,解决方法一是存一个IplImage的成员变量,在OnPaint里面去反复画它,另一个方法就是直接将其转换成CBitmap对像,并存于图像框中(或指定了图像样式的Static框中)。而IplImage转成CBitmap, 之前有转载文章说过,利用兼容DC和兼容位图来创建一幅等大小的CBitmap,然后用CvvImage将IplImage的图片画到DC上,然后将这个CBitmap设为图像框的显示图片,这样做的麻烦之处就在于要引入DC,经实验,可以直接创建一个CBitmap对象,将IplImage的数据填充到CBitmap的数据区, 因现在显示一般都用32位真彩色,故在往CBitmap中填充数据格式时,须将相就的字节填充完才可正常在设备上显示我们指定的图片。下面为该实现方法:
void CThermoGraphDlg::IplImageToCBitmap(IplImage* pImg, CBitmap** pBitmap) {if (*pBitmap) { delete *pBitmap; *pBitmap = NULL; *pBitmap = new CBitmap(); } DEVMODE stDevmode; EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &stDevmode); IplImage* pTmpImg = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U,stDevmode.dmBitsPerPel / IPL_DEPTH_8U); cvSetZero(pTmpImg); for (int i=0; iheight; i++) { for (int j=0; j width; j++) { int nIndex = j; CvScalar scaler = cvGetAt(pImg, i, j); cvSetAt(pTmpImg, scaler, i, j); } }int nLen = pTmpImg->imageSize; BYTE* pBytes = new BYTE[nLen]; memcpy(pBytes, pTmpImg->imageData, nLen); (*pBitmap)->CreateBitmap(pTmpImg->width, pTmpImg->height, 1, pTmpImg->nChannels*pTmpImg->depth, pBytes); delete [] pBytes; pBytes = NULL;}
if (GetDlgItem(IDC_STATIC_SHOW)) { if (!m_pBitmap) m_pBitmap = new CBitmap(); IplImageToCBitmap(m_pImgShow, &m_pBitmap); CStatic* pStatic = (CStatic*)GetDlgItem(IDC_STATIC_SHOW); pStatic->SetBitmap((HBITMAP)(*m_pBitmap)); }
文章出处:http://wglnngt-001.blog.163.com/blog/static/4077058420105185245802/