WIN32 控件显示RGB 图像 / Mat 图像

转载出处: http://blog.csdn.NET/zfdxx369/article/details/8138706


显示RGB图像:

[cpp]  view plain  copy
  1. void ShowRGBToWnd(HWND hWnd, BYTE* data, int width, int height)  
  2. {  
  3.     if (data == NULL)  
  4.         return;  
  5.   
  6.     static BITMAPINFO *bitMapinfo = NULL;  
  7.     static bool First = TRUE;  
  8.   
  9.     if (First)  
  10.     {  
  11.         bitBuffer = new BYTE[40 + 4 * 256];//开辟一个内存区域  
  12.   
  13.         if (bitBuffer == NULL)  
  14.         {  
  15.             return;  
  16.         }  
  17.         First = FALSE;  
  18.         memset(bitBuffer, 0, 40 + 4 * 256);  
  19.         bitMapinfo = (BITMAPINFO *)bitBuffer;  
  20.         bitMapinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);  
  21.         bitMapinfo->bmiHeader.biPlanes = 1;  
  22.         for (int i = 0; i < 256; i++)  
  23.         { //颜色的取值范围 (0-255)  
  24.             bitMapinfo->bmiColors[i].rgbBlue = bitMapinfo->bmiColors[i].rgbGreen = bitMapinfo->bmiColors[i].rgbRed = (BYTE)i;  
  25.         }  
  26.     }  
  27.     bitMapinfo->bmiHeader.biHeight = -height;  
  28.     bitMapinfo->bmiHeader.biWidth = width;  
  29.     bitMapinfo->bmiHeader.biBitCount = 3 * 8;  
  30.     CRect drect;  
  31.     GetClientRect(hWnd, drect);    //pWnd指向CWnd类的一个指针   
  32.     HDC hDC = GetDC(hWnd);     //HDC是Windows的一种数据类型,是设备描述句柄;  
  33.     SetStretchBltMode(hDC, COLORONCOLOR);  
  34.     StretchDIBits(hDC,  
  35.         0,  
  36.         0,  
  37.         drect.right,   //显示窗口宽度  
  38.         drect.bottom,  //显示窗口高度  
  39.         0,  
  40.         0,  
  41.         width,      //图像宽度  
  42.         height,      //图像高度  
  43.         data,  
  44.         bitMapinfo,  
  45.         DIB_RGB_COLORS,  
  46.         SRCCOPY  
  47.         );  
  48.     ReleaseDC(hWnd, hDC);  
  49. }  


显示Mat图像

[cpp]  view plain  copy
  1. void ShowMatImgToWnd(CWnd* pWnd, cv::Mat img)  
  2. {     
  3.     if(img.empty())    
  4.         return;    
  5.   
  6.   
  7.     static BITMAPINFO *bitMapinfo = NULL;  
  8.     static bool First=TRUE;  
  9.     if(First)  
  10.     {         
  11.         BYTE *bitBuffer = new BYTE[40+4*256];  
  12.         if(bitBuffer == NULL)  
  13.         {     
  14.             return;  
  15.         }  
  16.         First=FALSE;  
  17.         memset(bitBuffer, 0, 40+4*256);  
  18.         bitMapinfo = (BITMAPINFO *)bitBuffer;  
  19.         bitMapinfo->bmiHeader.biSize         = sizeof(BITMAPINFOHEADER);  
  20.         bitMapinfo->bmiHeader.biPlanes           = 1;      // 目标设备的级别,必须为1  
  21.         for(int i=0; i<256; i++)  
  22.         {   //颜色的取值范围 (0-255)  
  23.             bitMapinfo->bmiColors[i].rgbBlue  =bitMapinfo->bmiColors[i].rgbGreen =bitMapinfo->bmiColors[i].rgbRed   =(BYTE) i;  
  24.         }     
  25.       
  26.     }  
  27.       
  28.     bitMapinfo->bmiHeader.biHeight           = -img.rows;  //如果高度为正的,位图的起始位置在左下角。如果高度为负,起始位置在左上角。  
  29.     bitMapinfo->bmiHeader.biWidth            = img.cols;  
  30.     bitMapinfo->bmiHeader.biBitCount     = img.channels() *8;     // 每个像素所需的位数,必须是1(双色), 4(16色),8(256色)或24(真彩色)之一  
  31.   
  32.     CRect drect;         
  33.     pWnd->GetClientRect(drect);    //(drect);  (&drect);  两种方式均可,竟然    
  34.       
  35.     CClientDC dc(pWnd);  
  36.     HDC hDC =dc.GetSafeHdc();  
  37.     SetStretchBltMode(hDC, COLORONCOLOR);  //此句不能少哦  
  38.     //内存中的图像数据拷贝到屏幕上  
  39.     StretchDIBits(hDC,  
  40.                     0,  
  41.                     0,  
  42.                     drect.right,        //显示窗口宽度  
  43.                     drect.bottom,       //显示窗口高度  
  44.                     0,  
  45.                     0,  
  46.                     img.cols,          //图像宽度  
  47.                     img.rows,          //图像高度  
  48.                     img.data,             
  49.                     bitMapinfo,           
  50.                     DIB_RGB_COLORS,   
  51.                     SRCCOPY  
  52.                   );  
  53.       
  54.       
  55. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值