作者:hardVB
转自:http://blog.youkuaiyun.com/hardvb/article/details/1575349
IplImage* pImg;
CString strPathName;
CFileDialog dlg(TRUE);
CDC
*
pDC
=
this
->
GetDC();
CRect rect;
this
->
GetClientRect(
&
rect);
if
(dlg.DoModal()
==
IDOK)
{
strPathName = dlg.GetPathName();

pImg = cvLoadImage(strPathName);
BITMAPINFO bmi;
FillBitmapInfo(&bmi,pImg->width,pImg->height,(pImg->depth)*(pImg->nChannels));
::StretchDIBits(pDC->GetSafeHdc(),rect.left,rect.top,pImg->width,pImg->height,0,0,pImg->width,pImg->height,pImg->imageData,&bmi,DIB_RGB_COLORS,SRCCOPY);
}

void
COpenCVDrawView::FillBitmapInfo( BITMAPINFO
*
bmi,
int
width,
int
height,
int
bpp )
{
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 = -abs(height);
bmih->biPlanes = 1;
bmih->biBitCount = 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;
}
}
}