MFC通过OpenCV 显示到Picture control


编写了图像显示的类,只需要把控件ID和文件名以及句柄传送到函数,就可以显示图像,简单示例,供大家学习,VS2010+Opencv2.4.9
资源截图
代码片段和文件信息
// ImgToControls.cpp : implementation file
//

#include “stdafx.h“
#include “PaintImgToScreenControls.h“
#include “ImgToControls.h“
#include “afxdialogex.h“


// ImgToControls dialog

IMPLEMENT_DYNAMIC(ImgToControls CDialogEx)

ImgToControls::ImgToControls(CWnd* pParent /*=NULL*/)
: CDialogEx(ImgToControls::IDD pParent)
{

}

ImgToControls::~ImgToControls()
{
}

void ImgToControls::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(ImgToControls CDialogEx)
END_MESSAGE_MAP()


// ImgToControls message handlers



void  ImgToControls::drawpicinit(IplImage* img unsigned int idCWnd* showWnd)  
{  
//位图信息初始化  
bmibuf = new BYTE[sizeof(BITMAPINFO)+256 * sizeof(RGBQUAD)];  
memset(bmibuf 0 sizeof(bmibuf));  
pbmi = (BITMAPINFO*)bmibuf;  
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);  
pbmi->bmiHeader.biWidth = img->width;  
pbmi->bmiHeader.biHeight = img->height;  
pbmi->bmiHeader.biPlanes = 1;  
pbmi->bmiHeader.biBitCount = 24;  
pbmi->bmiHeader.biCompression = BI_RGB;   
mShowWnd = showWnd;  
pDC = mShowWnd->GetDlgItem(id)->GetDC();  
pic = (CStatic*)mShowWnd->GetDlgItem(id);  
pic->GetClientRect(&rect);  

g_hMemDC = CreateCompatibleDC(pDC->m_hDC);//创建兼容设备环境的内存DC  
g_hBmp = CreateDIBSection(g_hMemDC pbmi DIB_RGB_COLORS (void**)&g_pBits 0 0);//创建应用程序可以直接写入的、与设备无关的位图  
}  

void ImgToControls::drawpic(IplImage* img)//CMFCOpenCVShowDlg 为对话框类名  
{  
//修改图像内容:g_pBits  
//这里这么做一则为BMP图像的四字节对齐机制,二则是因为BMP图像是从图像的左下角开始算起的,如果直接拷贝会导致图像上下颠倒  
int l_width = WIDTHBYTES(img->width* pbmi->bmiHeader.biBitCount);  
for (int row = 0; row < img->height; row++)  
memcpy(&g_pBits[row*l_width] &img->imageData[(img->height - row - 1)*l_width] l_width);  

Selectobject(g_hMemDC g_hBmp);//将位图对象选入g_hMemDC内存DC中  
//拉伸绘图  
SetStretchBltMode(pDC->m_hDC HALFTONE);  
StretchBlt(pDC->m_hDC 0 0 rect.Width() rect.Height() g_hMemDC 0 0 img->width img->height SRCCOPY);  
}  

void ImgToControls::drawrelease()  
{  
//释放内存资源      
delete[]bmibuf;  
DeleteDC(g_hMemDC);  
Deleteobject(pic);  
Deleteobject(g_hBmp);  
mShowWnd->ReleaseDC(pDC);  
}  

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3418  2017-11-27 15:09  PaintImgToScreenControlsDebugcl.command.1.tlog

     文件     100728  2017-11-27 15:09  PaintImgToScreenControlsDebugCL.read.1.tlog

     文件       2660  2017-11-27 15:09  PaintImgToScreenControlsDebugCL.write.1.tlog

     文件     129658  2017-11-27 15:09  PaintImgToScreenControlsDebugImgToControls.obj

     文件          2  2017-11-27 15:09  PaintImgToScreenControlsDebuglink-cvtres.read.1.tlog

     文件          2  2017-11-27 15:09  PaintImgToScreenControlsDebuglink-cvtres.write.1.tlog

     文件       3310  2017-11-27 15:09  PaintImgToScreenControlsDebuglink.command.1.tlog

     文件       9042  2017-11-27 15:09  PaintImgToScreenControlsDebuglink.read.1.tlog

     文件       1754  2017-11-27 15:09  PaintImgToScreenControlsDebuglink.write.1.tlog

     文件        884  2017-11-27 15:09  PaintImgToScreenControlsDebugmt.command.1.tlog

     文件        598  2017-11-27 15:09  PaintImgToScreenControlsDebugmt.read.1.tlog

     文件        634  2017-11-27 15:09  PaintImgToScreenControlsDebugmt.write.1.tlog

     文件       2599  2017-11-27 15:09  PaintImgToScreenControlsDebugPaintImgToScreenControls.Build.CppClean.log

     文件        667  2017-11-27 15:09  PaintImgToScreenControlsDebugPaintImgToScreenControls.exe.embed.manifest

     文件        732  2017-11-27 15:09  PaintImgToScreenControlsDebugPaintImgToScreenControls.exe.embed.manifest.res

     文件        381  2017-11-27 15:09  PaintImgToScreenControlsDebugPaintImgToScreenControls.exe.intermediate.manifest

     文件         60  2017-11-27 15:09  PaintImgToScreenControlsDebugPaintImgToScreenControls.lastbuildstate

     文件       5347  2017-11-27 15:09  PaintImgToScreenControlsDebugPaintImgToScreenControls.log

     文件      30321  2017-11-27 15:09  PaintImgToScreenControlsDebugPaintImgToScreenControls.obj

     文件   33816576  2017-11-27 15:09  PaintImgToScreenControlsDebugPaintImgToScreenControls.pch

     文件      69992  2017-11-27 15:09  PaintImgToScreenControlsDebugPaintImgToScreenControls.res

     文件          0  2017-11-27 15:09  PaintImgToScreenControlsDebugPaintImgToScreenControls.write.1.tlog

     文件     146879  2017-11-27 15:09  PaintImgToScreenControlsDebugPaintImgToScreenControlsDlg.obj

     文件        238  2017-11-27 15:09  PaintImgToScreenControlsDebugPaintImgToScreenControls_manifest.rc

     文件       1528  2017-11-27 15:09  PaintImgToScreenControlsDebug
c.command.1.tlog

     文件       3910  2017-11-27 15:09  PaintImgToScreenControlsDebug
c.read.1.tlog

     文件        798  2017-11-27 15:09  PaintImgToScreenControlsDebug
c.write.1.tlog

     文件     641909  2017-11-27 15:09  PaintImgToScreenControlsDebugstdafx.obj

     文件    2460672  2017-11-27 15:09  PaintImgToScreenControlsDebugvc100.idb

     文件    4386816  2017-11-27 15:09  PaintImgToScreenControlsDebugvc100.pdb

............此处省略42个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)