iplimage 转HBITMAP

HBITMAP  IplImage2hBitmap(IplImage* pImg)
{
    cvFlip(pImg, NULL);
    BYTE tmp[sizeof(BITMAPINFO)+1024];
    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 = 0;//if biCompression is BI_RGB,this can be 0
    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;
        ((DWORD*) bmi->bmiColors)[1] = 0x0000FF00;
        ((DWORD*) bmi->bmiColors)[2] = 0x000000FF;
        break; 
    } 
    hBmp = ::CreateDIBSection(NULL,bmi,DIB_RGB_COLORS,NULL,0,0);

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

    return hBmp;
}

 

posted @ 2016-01-19 15:20  QQ76211822  阅读(526)  评论(0编辑  收藏  举报