HBitmap 转Mat

bool HBitmap2Mat(HBITMAP &hBmp,Mat &mat)

{
BITMAP Bmp;
GetObject(a->bmp,sizeof(BITMAP),&Bmp);
int nChannels = Bmp.bmBitsPixel == 1 ? 1 : Bmp.bmBitsPixel/8 ;
int depth = Bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;

Mat des_mat;

des_mat.create(cvSize(Bmp.bmWidth,Bmp.bmHeight), CV_MAKETYPE(CV_8U,nChannels));
GetBitmapBits(a->bmp,Bmp.bmHeight*Bmp.bmWidth*nChannels,des_mat.data);

mat = des_mat;

return true;

}

Mat 转HBitmap

bool Mat2HBitmap(HBITMAP &hBmp,Mat &mat)

{
//MAT类的TYPE=(nChannels-1+ CV_8U)<<3int nChannels=(mat.type()>>3)-CV_8U+1;

int iSize=mat.cols*mat.rows*nChannels;
hBmp=CreateBitmap( mat.cols,mat.rows,1,nChannels*8,mat.data);
return true;

}