将IplImage转换为DIB

Opencv中将IplImage转换为DIB:

 

HBITMAP CGameDlg::IplIamgeToDIB(IplImage *pImg,int Size)

{



	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 = Size;

	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;



}

此文章只供参考,转载请注明:本文作者:longlongago     博客地址:http://blog.csdn.net/longlongago2000

有问题可留言或发邮件讨论。

你可能感兴趣的:(null,BI,byte,colors)