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

6 篇文章 0 订阅
2 篇文章 0 订阅

 

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

PictureCtrl 显示Mat图像: https://blog.csdn.net/fjqlldg/article/details/52260585

在OnInitDialog 或者 OnInitialUpdate 中添加

        namedWindow("show",WINDOW_AUTOSIZE);
	HWND hWnd = (HWND)cvGetWindowHandle("show");
	HWND hParent = ::GetParent(hWnd);
	::SetParent(hWnd,GetDlgItem(IDC_STATIC_PICTURE)->GetSafeHwnd());
	::ShowWindow(hParent,SW_HIDE);

在需要显示的地方添加

        Mat image=imread(picturePath);     
	Mat show;  
	//以下操作获取图形控件尺寸并以此改变图片尺寸  
	CRect rect;  
	GetDlgItem(IDC_STATIC_PICTURE)->GetClientRect(&rect);  
	resize(image,show,Size(rect.Width(),rect.Height()));   
	imshow("show",show);

显示RGB图像:

 

void ShowRGBToWnd(HWND hWnd, BYTE* data, int width, int height)
{
	if (data == NULL)
		return;

	static BITMAPINFO *bitMapinfo = NULL;
	static bool First = TRUE;

	if (First)
	{
		bitBuffer = new BYTE[40 + 4 * 256];//开辟一个内存区域

		if (bitBuffer == NULL)
		{
			return;
		}
		First = FALSE;
		memset(bitBuffer, 0, 40 + 4 * 256);
		bitMapinfo = (BITMAPINFO *)bitBuffer;
		bitMapinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		bitMapinfo->bmiHeader.biPlanes = 1;
		for (int i = 0; i < 256; i++)
		{ //颜色的取值范围 (0-255)
			bitMapinfo->bmiColors[i].rgbBlue = bitMapinfo->bmiColors[i].rgbGreen = bitMapinfo->bmiColors[i].rgbRed = (BYTE)i;
		}
	}
	bitMapinfo->bmiHeader.biHeight = -height;
	bitMapinfo->bmiHeader.biWidth = width;
	bitMapinfo->bmiHeader.biBitCount = 3 * 8;
	CRect drect;
	GetClientRect(hWnd, drect);    //pWnd指向CWnd类的一个指针 
	HDC hDC = GetDC(hWnd);     //HDC是Windows的一种数据类型,是设备描述句柄;
	SetStretchBltMode(hDC, COLORONCOLOR);
	StretchDIBits(hDC,
		0,
		0,
		drect.right,   //显示窗口宽度
		drect.bottom,  //显示窗口高度
		0,
		0,
		width,      //图像宽度
		height,      //图像高度
		data,
		bitMapinfo,
		DIB_RGB_COLORS,
		SRCCOPY
		);
	ReleaseDC(hWnd, hDC);
}

 

 

 

 

 

显示Mat图像

 

void ShowMatImgToWnd(CWnd* pWnd, cv::Mat img)
{	
    if(img.empty())  
        return;  


	static BITMAPINFO *bitMapinfo = NULL;
	static bool First=TRUE;
	if(First)
	{		
		BYTE *bitBuffer	= new BYTE[40+4*256];
		if(bitBuffer == NULL)
		{	
			return;
		}
		First=FALSE;
		memset(bitBuffer, 0, 40+4*256);
		bitMapinfo = (BITMAPINFO *)bitBuffer;
		bitMapinfo->bmiHeader.biSize			= sizeof(BITMAPINFOHEADER);
		bitMapinfo->bmiHeader.biPlanes			= 1;      // 目标设备的级别,必须为1
		for(int i=0; i<256; i++)
		{	//颜色的取值范围 (0-255)
			bitMapinfo->bmiColors[i].rgbBlue  =bitMapinfo->bmiColors[i].rgbGreen =bitMapinfo->bmiColors[i].rgbRed   =(BYTE) i;
		}	
	
	}
	
	bitMapinfo->bmiHeader.biHeight		    = -img.rows;  //如果高度为正的,位图的起始位置在左下角。如果高度为负,起始位置在左上角。
	bitMapinfo->bmiHeader.biWidth		    = img.cols;
	bitMapinfo->bmiHeader.biBitCount		= img.channels() *8;     // 每个像素所需的位数,必须是1(双色), 4(16色),8(256色)或24(真彩色)之一

    CRect drect;       
    pWnd->GetClientRect(drect);    //(drect);  (&drect);  两种方式均可,竟然	
	
	CClientDC dc(pWnd);
	HDC hDC =dc.GetSafeHdc();
	SetStretchBltMode(hDC, COLORONCOLOR);  //此句不能少哦
	//内存中的图像数据拷贝到屏幕上
	StretchDIBits(hDC,
					0,
					0,
					drect.right,		//显示窗口宽度
					drect.bottom,		//显示窗口高度
					0,
					0,
					img.cols,		   //图像宽度
					img.rows,		   //图像高度
					img.data,			
					bitMapinfo,			
					DIB_RGB_COLORS, 
					SRCCOPY
				  );
	
	
}

 

 

 

 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值