16,548
社区成员




void ShowCvImage(HWND hWnd, const IplImage* pImg)
{
CV_FUNCNAME("ShowCvImage" );
FUN_BEGIN
SIZE size = { 0, 0 };
int channels = 0;
void* dst_ptr = 0;
const int channels0 = 3;
int origin = 0;
CvMat stub, dst, *image;
bool changed_size = false;
CRect rx;
if(!pImg ) EXIT_FUN;
if(!hWnd) EXIT_FUN;
GetClientRect(hWnd,&rx);
HDC hdcX =GetDC(hWnd);
HDC hdc=CreateCompatibleDC(hdcX);
HBITMAP hBmp=CreateCompatibleBitmap(hdcX,rx.Width(),rx.Height());
HGDIOBJ hOldBmp=SelectObject(hdc,hBmp);
HBRUSH hbr=CreateSolidBrush(RGB(255,255,255));
FillRect(hdc,&rx,hbr);
DeleteObject(hbr);
if (hdc)
{
//GetBitmapData
BITMAP bmp;
//GdiFlush();
//HGDIOBJ hBmp = GetCurrentObject(hdc, OBJ_BITMAP );
HBITMAP hBmp =CreateCompatibleBitmap(hdc,1,1);// GetCurrentObject(hdc, OBJ_BITMAP );
if (hBmp == NULL) EXIT_FUN;
if(::GetObject(hBmp,sizeof(bmp), &bmp) == 0)
{
int nErr=GetLastError();
EXIT_FUN;//GetObject(): returns size of object, 0 if error
}
channels = bmp.bmBitsPixel/8;
DeleteObject(hBmp);
if( CV_IS_IMAGE_HDR( pImg )) origin = pImg->origin;
//CV_CALL(image = cvGetMat(pImg, &stub ));
image = cvGetMat(pImg, &stub );
if( size.cx != image->width || size.cy != image->height || channels != channels0 )
{
changed_size = true;
uchar buffer[sizeof(BITMAPINFO) + 255*sizeof(RGBQUAD)];
BITMAPINFO* pInfo = (BITMAPINFO*)buffer;
BOOL bDeleteObj = DeleteObject(GetCurrentObject(hdc, OBJ_BITMAP));
CV_Assert( FALSE != bDeleteObj );
//size.cx = image->width;
//size.cy = image->height;
channels = channels0;
FillBitmapInfo(pInfo, image->width,image->height, channels*8, 1 );
//SelectObject(hdc, CreateDIBSection( hdc,binfo, DIB_RGB_COLORS, &dst_ptr, 0, 0));
HBITMAP hDibBmp=CreateDIBSection( hdc,pInfo, DIB_RGB_COLORS, &dst_ptr, 0, 0);
cvInitMatHeader(&dst, image->height,image->width, CV_8UC3, dst_ptr, (image->width * channels + 3) & -4 );
cvConvertImage(image, &dst, origin == 0 ? CV_CVTIMG_FLIP : 0 );
// Image stretching to fit the window
//RECT rect;
//GetClientRect(hWnd, &rect);
SetStretchBltMode(hdc,HALFTONE);
//StretchDIBits(hdc, 0, 0, rect.right, rect.bottom, 0, 0, image->width, image->height, dst_ptr, &binfo, DIB_RGB_COLORS, SRCCOPY );
SetDIBitsToDevice(hdc,0,0,rx.Width(),image->height/*rx.Height()*/,0,0,0,image->height-1, dst_ptr,pInfo, DIB_RGB_COLORS);
//SetDIBits(hdc,0,0,image->height,dst_ptr, &binfo, DIB_RGB_COLORS);
DeleteObject(hDibBmp);
}
BitBlt(hdcX,0,0,rx.Width(),rx.Height(),hdc,0,0,SRCCOPY);
SelectObject(hdc,hOldBmp);
DeleteObject(hBmp);
DeleteDC(hdc);
ReleaseDC(hWnd,hdcX);
}
FUN_FINALLY
}
#ifdef _DEBUG
#pragma comment(lib,"opencv_core246d.lib")
#pragma comment(lib,"opencv_imgproc246d.lib")
#pragma comment(lib,"opencv_highgui246d.lib")
#pragma comment(lib,"opencv_ml246d.lib")
#pragma comment(lib,"opencv_video246d.lib")
#pragma comment(lib,"opencv_features2d246d.lib")
#pragma comment(lib,"opencv_calib3d246d.lib")
#pragma comment(lib,"opencv_objdetect246d.lib")
#pragma comment(lib,"opencv_contrib246d.lib")
#pragma comment(lib,"opencv_legacy246d.lib")
#pragma comment(lib,"opencv_flann246d.lib")
#else
#pragma comment(lib,"opencv_core246.lib")
#pragma comment(lib,"opencv_imgproc246.lib")
#pragma comment(lib,"opencv_highgui246.lib")
#pragma comment(lib,"opencv_ml246.lib")
#pragma comment(lib,"opencv_video246.lib")
#pragma comment(lib,"opencv_features2d246.lib")
#pragma comment(lib,"opencv_calib3d246.lib")
#pragma comment(lib,"opencv_objdetect246.lib")
#pragma comment(lib,"opencv_contrib246.lib")
#pragma comment(lib,"opencv_legacy246.lib")
#pragma comment(lib,"opencv_flann246.lib")
#endif
这样,就不会报告泄漏了。
问题虽然解决,但是不明就里,欢迎各位新老高手踊跃讨论