您的位置:首页 > 其它

Picture control显示图像及Mat转换为CImage

2014-07-23 22:29 267 查看
(以VS2010为例)

1. 点击资源, 选择Dialog并点开, 在任意对话资源上右击, 选择"插入"Dialog", 如图1所示.





2. 插入后, 默认ID为IDD_DIALOG1, 可修改为自己相应的ID, 修改方式为:选择"属性"(可右击资源, 选择属性, 也可以选中资源后, 点右上的"属性")





3. 选择工具箱->Picture Control 控件, 并将Picture Control控件拖到对话框上.








拖上去后, 可做相关属性的修改. 如可以其ID修改为IDC_MY_PIC(下面的程序以将其ID修改为IDC_MY_PIC为例).
4. 在适当位置添加如下代码

CImage myImage;
myImage.Load(_T("d:\\lena.bmp"));

CRect rect;
CWnd *pWnd = GetDlgItem(IDC_MY_PIC); (这是在此资源创建的类的内部, 若是在外部, 可先通过获得CMainFrame的指针, 再通过pMianFrame->GetDlgItem(IDCk_MY_PIC)来获取)
CDC *pDC = pWnd->GetDC();
pWnd->GetClientRect(&rect);
pDC->SetStretchBltMode(STRETCH_HALFTONE);
myImage.Draw(pDC->m_hDC, rect);
ReleaseDC(pDC);
myImage.Destroy();

Mat转换为CImage:

[cpp] view
plaincopy

void MatToCImage( Mat &mat, CImage &cImage)

{

//create new CImage

int width = mat.cols;

int height = mat.rows;

int channels = mat.channels();



cImage.Destroy(); //clear

cImage.Create(width,

height, //positive: left-bottom-up or negative: left-top-down

8*channels ); //numbers of bits per pixel



//copy values

uchar* ps;

uchar* pimg = (uchar*)cImage.GetBits(); //A pointer to the bitmap buffer



//The pitch is the distance, in bytes. represent the beginning of

// one bitmap line and the beginning of the next bitmap line

int step = cImage.GetPitch();



for (int i = 0; i < height; ++i)

{

ps = (mat.ptr<uchar>(i));

for ( int j = 0; j < width; ++j )

{

if ( channels == 1 ) //gray

{

*(pimg + i*step + j) = ps[j];

}

else if ( channels == 3 ) //color

{

for (int k = 0 ; k < 3; ++k )

{

*(pimg + i*step + j*3 + k ) = ps[j*3 + k];

}

}

}

}



}

转自:http://vipjy2008.blog.163.com/blog/static/372087672014026314586/ http://blog.csdn.net/merlin_q/article/details/7041040/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: