1、 在窗口中拖放控件CList,同时并添加两个变量
CListCtrlm_list;//模板图像列表
CImageList*m_pImgList;//模板图像
2、 初始化列表控件
m_list.DeleteAllItems();
//关于模板图像列表的初始化
m_pImgList=new CImageList;
CWnd *cWnd = GetDlgItem(IDC_MODEL_IMAGE_LIST);//获得控件
CRect rect;
cWnd->GetWindowRect(rect);
m_pImgList->Create(rect.Width() /6,rect.Height() * 0.56,ILC_COLOR32,20,20);//前两个参数是设置图象大小的,nInitial用来确定图像列表包含的图像数量;nGrow用来确定图像列表可控制的图像数量。
m_pImgList->SetBkColor(RGB(125,125,0));
m_list.SetImageList(m_pImgList,0);//将设置的位图列表选入ListView中去
3、 向CList中添加图像
while(!stringList->IsEmpty())//stringList是获得图像文件列表
{
fileName =stringList->RemoveHead();
/*显示位图文件*/
CBitmap *pBitmap;
m_list.InsertItem(m_picNum, fileName,m_picNum);
IplImage* image;
//使用opencv操作图像
image = cvLoadImage(cutPicPath +fileName,1); //IplImage2Bitmap
IplImage * scalarImage =cvCreateImage(cvSize(rect.Width() / 6,rect.Height() * 0.56), image->depth,image->nChannels);//符合初始化大小的图片
cvResize(image, scalarImage);
CCigManageSysDlg cCigManageSysDlg =new CCigManageSysDlg();
HDC hDC = GetDC()->GetSafeHdc();
pBitmap =cCigManageSysDlg.IplImage2CBitmap(hDC, scalarImage);
m_pImgList->Add (pBitmap,RGB(0,0,0));
//增加图片数量
m_picNum++;
}
附IplImage2CBitmap代码:
CBitmap*CCigManageSysDlg::IplImage2CBitmap(HDC hDC, constIplImage *pImage)
{
if( pImage&& pImage->depth == IPL_DEPTH_8U )
{
uchar buffer[sizeof(BITMAPINFOHEADER)+ 1024];
BITMAPINFO* bmi = (BITMAPINFO*)buffer;
intbmp_w = pImage->width, bmp_h = pImage->height;
FillBitmapInfo( bmi, bmp_w, bmp_h,pImage->depth*pImage->nChannels, pImage->origin );
char*pBits=NULL;
HBITMAPhBitmap=CreateDIBSection(hDC,bmi,DIB_RGB_COLORS,(void**)&pBits,NULL,0);
memcpy(pBits,pImage->imageData,pImage->imageSize);
CBitmap *pBitmap=new CBitmap;
pBitmap->Attach(hBitmap);
returnpBitmap;
}
else
returnNULL;
}
void CCigManageSysDlg::FillBitmapInfo( BITMAPINFO*bmi, int width, intheight, int bpp, intorigin)
{
assert( bmi && width >= 0&& height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));
BITMAPINFOHEADER* bmih =&(bmi->bmiHeader);
memset( bmih, 0, sizeof(*bmih));
bmih->biSize = sizeof(BITMAPINFOHEADER);
bmih->biWidth = width;
bmih->biHeight = origin ? abs(height) :-abs(height);
bmih->biPlanes = 1;
bmih->biBitCount = (unsigned short)bpp;
bmih->biCompression = BI_RGB;
if( bpp == 8)
{
RGBQUAD* palette = bmi->bmiColors;
int i;
for( i =0; i < 256; i++ )
{
palette[i].rgbBlue =palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
palette[i].rgbReserved = 0;
}
}
}