vs2013+opencv Mat图像显示到picture control控件

本文介绍了一个用于枪支编码识别的对话框类,该类包含一个关键函数showMatImgToWnd,用于将OpenCV的Mat图像加载并显示到指定的Picture Control控件中。文章详细展示了如何在对话框中实现图像的正确显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dlg.h头文件中添加函数声明

class C枪支编码识别Dlg : public CDialogEx
{
// 构造
public:
	C枪支编码识别Dlg(CWnd* pParent = NULL);	// 标准构造函数

// 对话框数据
	enum { IDD = IDD_MY_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV 支持

	void C枪支编码识别Dlg::showMatImgToWnd(CWnd* pictureWnd, const Mat& disimg);     //函数声明

Dlg.cpp中添加下面的函数

(第一个参数为picture control控件句柄,后一个参数为要显示的Mat图像)

void C枪支编码识别Dlg::showMatImgToWnd(CWnd* pictureWnd, const Mat& disimg)//Mat图像加载到picture control函数
{
	if (disimg.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;
		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 = -disimg.rows;
	bitMapinfo->bmiHeader.biWidth = disimg.cols;
	bitMapinfo->bmiHeader.biBitCount = disimg.channels() * 8;

	CRect drect;
	pictureWnd->GetClientRect(drect);    //pWnd指向CWnd类的一个指针 
	CClientDC dc(pictureWnd);
	HDC hDC = dc.GetSafeHdc();                  //HDC是Windows的一种数据类型,是设备描述句柄;
	SetStretchBltMode(hDC, COLORONCOLOR);
	StretchDIBits(hDC,
		0,
		0,
		drect.right,  //显示窗口宽度
		drect.bottom,  //显示窗口高度
		0,
		0,
		disimg.cols,     //图像宽度
		disimg.rows,     //图像高度
		disimg.data,
		bitMapinfo,
		DIB_RGB_COLORS,
		SRCCOPY);
}

获取picture control控件句柄

CWnd *hwnd = GetDlgItem(IDC_STATIC);


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值