opencv结合MFC显示带滚动条的大图片

标签:
opencvmfc滚动条大图 |
分类: ITprogrammer |
参考 http://blog.csdn.net/ixzf2009/article/details/6564456
的内容,结合自己的实际情况,实现了在MFC中显示IplImage 的大图片。
http://s1/mw690/8154bb46t02b367313a00&690
1. 工程是在VS2010中的基于对话框的MFC工程。
我最开始的思路是创建一个CStatic的图片控件,为其添加滚动条,发现无法实现。
此工程中的思路是创建一个BUTTON,通过为BUTTON添加背景图案来实现。
下面进行必要的流程阐述和相关代码。
A。新建一个继承自CBUTTON的ButtonPic类。
在ButtonPic.h中添加变量和函数声明
// Attributes
public:
int
m_nHorzPos;
int
m_nVertPos;
int
m_nHorzMaxSize;
int
m_nVertMaxSize;
int
m_nHorzPageSize;
int
m_nVertPageSize;
int
m_nOneStep;
CBitmap
*cb;
public:
virtual void
DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
void
InitPic(int nStep,CBitmap *cbmp);
void
DarwPic();
B。 在对话框中添加一个BUTTON,为其关联变量,注意,变量类型需要为ButtonPic,此处变量名为m_cbtnPic
在 BOOL CDispScrollDlg::OnInitDialog()中添加代码:
// TODO: Add extra initialization here
m_cbtnPic.ShowScrollBar(SB_BOTH);
CDC* pDC =
this
->GetDC();
// 获得显示控件的 DC
HDC hDC =
pDC
->GetSafeHdc();
// 获取 HDC(设备句柄) 来进行绘图操作
IplImage
*image=cvLoadImage("lena.jpg");
CBitmap
*cbmp=IplImage2CBitmap(image);
m_cbtnPic.InitPic(10,cbmp);
此处IplImage2CBitmap是用来将IplImage类型转成CBitmap类型。
代码为:
CBitmap * CDispScrollDlg::IplImage2CBitmap(const IplImage *pImage)
{
if( pImage
&& pImage->depth ==
IPL_DEPTH_8U )
{
HDC
hDC=this->GetDC()->GetSafeHdc();
uchar
buffer[sizeof(BITMAPINFOHEADER) + 1024];
BITMAPINFO*
bmi = (BITMAPINFO*)buffer;
int bmp_w =
pImage->width, bmp_h =
pImage->height;
FillBitmapInfo( bmi, bmp_w, bmp_h,
pImage->depth*pImage->nChannels,
pImage->origin );
char
*pBits=NULL;
HBITMAP
hBitmap=CreateDIBSection(hDC,bmi,DIB_RGB_COLORS,(void**)&pBits,NULL,0);
memcpy(pBits,pImage->imageData,pImage->imageSize);
CBitmap
*pBitmap=new CBitmap;
pBitmap->Attach(hBitmap);
return
pBitmap;
}
else
return
NULL;
}
void CDispScrollDlg::FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin)
{
assert( bmi
&& width >= 0
&& height >= 0
&& (bpp == 8 || bpp == 24 || bpp ==
32));
BITMAPINFOHEADER* bmih =
&(bmi->bmiHeader);
memset(
bmih, 0, sizeof(*bmih));
bmih->biSize = sizeof(BITMAPINFOHEADER);
bmih->biWidth = width;
bmih->biHeight = origin ? abs(height) :
-abs(height);
bmih->biPlanes = 1;
bmih->biBitCount = (unsigned short)bpp;
bmih->biCompression = BI_RGB;
if( bpp == 8
)
{
RGBQUAD*
palette = bmi->bmiColors;
int i;
for( i = 0;
i < 256; i++ )
{
palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed =
(BYTE)i;
palette[i].rgbReserved = 0;
}
}
}
ButtonPic.cpp 的代码贴在下方。
注意,其中两个函数OnVScroll,OnHScroll需要通过类向导建立。是用来控制滚动条的。
// ButtonPic.cpp : implementation file
//
#include "stdafx.h"
#include "DispScroll.h"
#include "ButtonPic.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CButtonPic
CButtonPic::CButtonPic()
{
}
CButtonPic::~CButtonPic()
{
}
BEGIN_MESSAGE_MAP(CButtonPic, CButton)
1. 工程是在VS2010中的基于对话框的MFC工程。
我最开始的思路是创建一个CStatic的图片控件,为其添加滚动条,发现无法实现。
此工程中的思路是创建一个BUTTON,通过为BUTTON添加背景图案来实现。
下面进行必要的流程阐述和相关代码。
A。新建一个继承自CBUTTON的ButtonPic类。
在ButtonPic.h中添加变量和函数声明
// Attributes
public:
public:
B。 在对话框中添加一个BUTTON,为其关联变量,注意,变量类型需要为ButtonPic,此处变量名为m_cbtnPic
在 BOOL CDispScrollDlg::OnInitDialog()中添加代码:
// TODO: Add extra initialization here
此处IplImage2CBitmap是用来将IplImage类型转成CBitmap类型。
代码为:
CBitmap * CDispScrollDlg::IplImage2CBitmap(const IplImage *pImage)
{
}
void CDispScrollDlg::FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin)
{
}
ButtonPic.cpp 的代码贴在下方。
注意,其中两个函数OnVScroll,OnHScroll需要通过类向导建立。是用来控制滚动条的。
// ButtonPic.cpp : implementation file
//
#include "stdafx.h"
#include "DispScroll.h"
#include "ButtonPic.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CButtonPic
CButtonPic::CButtonPic()
{
}
CButtonPic::~CButtonPic()
{
}
BEGIN_MESSAGE_MAP(CButtonPic, CButton)