将IplImage转换为DIB

Opencv中将IplImage转换为DIB:

 

HBITMAP CGameDlg::IplIamgeToDIB(IplImage *pImg)

{



	HDC hDC = ::CreateCompatibleDC(0);

	BYTE tmp[sizeof(BITMAPINFO)+255*4];

	BITMAPINFO *bmi = (BITMAPINFO*)tmp;

	HBITMAP hBmp;

	int i;

	memset(bmi,0,sizeof(BITMAPINFO));

	bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

	bmi->bmiHeader.biWidth = pImg->width;

	bmi->bmiHeader.biHeight = -pImg->height;

	bmi->bmiHeader.biPlanes = 1;

	bmi->bmiHeader.biBitCount = pImg->nChannels * pImg->depth;

	bmi->bmiHeader.biCompression = BI_RGB;

	bmi->bmiHeader.biSizeImage = pImg->width*pImg->height*1;

	bmi->bmiHeader.biClrImportant =0 ;

	switch(pImg->nChannels * pImg->depth)

	{

	case 8 :

	for(i=0 ; i < 256 ; i++)

	{

	bmi->bmiColors[i].rgbBlue = i;

	bmi->bmiColors[i].rgbGreen= i;

	bmi->bmiColors[i].rgbRed= i;

	}

	break;

	case 32:

	case 24:

	((DWORD*) bmi->bmiColors)[0] = 0x00FF0000; /* red mask */

	((DWORD*) bmi->bmiColors)[1] = 0x0000FF00; /* green mask */

	((DWORD*) bmi->bmiColors)[2] = 0x000000FF; /* blue mask */

	break;

	}

	hBmp = ::CreateDIBSection(hDC,bmi,DIB_RGB_COLORS,NULL,0,0);

	SetDIBits(hDC,hBmp,0,pImg->height,pImg->imageData,bmi,DIB_RGB_COLORS);

	::DeleteDC(hDC);

return hBmp;



}


文章出处:http://blog.csdn.net/longlongago2000/article/details/2583409

 

你可能感兴趣的:(将IplImage转换为DIB)