//将Mat绘制到DC上
void DrawMatToDC(HDC MemDC, const Mat& tmpMat)
{
uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
BITMAPINFO* bmi = (BITMAPINFO*)buffer;
int bmp_w = tmpMat.cols, bmp_h = tmpMat.rows;
FillBitmapInfo( bmi, bmp_w, bmp_h, tmpMat.channels() * 8, -1 );
int from_x = MIN( MAX( 0, 0 ), bmp_w - 1 );
int from_y = MIN( MAX( 0, 0 ), bmp_h - 1 );
int sw = MAX( MIN( bmp_w - 0, tmpmat.cols ), 0 );
int sh = MAX( MIN( bmp_h - 0, tmpmat.rows ), 0 );
SetDIBitsToDevice(
MemDC, 0, 0, sw, sh, from_x, from_y, from_y, sh,
tmpMat.data + from_y*tmpMat.channels(),
bmi, DIB_RGB_COLORS );
}
void FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin )
{
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 = origin ? abs(height) : -abs(height);
bmih->biPlanes = 1;
bmih->biBitCount = (unsigned short)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;
}
}
}
其中,需要将tmpmat中的row和col通过resize函数变成整数4的整数倍,才可以正确的绘制在HDC上