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

来源:互联网 发布:最终幻想15 amd优化 编辑:程序博客网 时间:2024/05/29 08:38
(以VS2010为例)
1. 点击资源, 选择Dialog并点开, 在任意对话资源上右击, 选择"插入"Dialog", 如图1所示.
MFC用Picture Control显示图像 - vipjy2008 - 乐观开朗的小孩
 
2. 插入后, 默认ID为IDD_DIALOG1, 可修改为自己相应的ID, 修改方式为:选择"属性"(可右击资源, 选择属性, 也可以选中资源后, 点右上的"属性")
MFC用Picture Control显示图像 - vipjy2008 - 乐观开朗的小孩
 
3. 选择工具箱->Picture Control 控件, 并将Picture Control控件拖到对话框上.
MFC用Picture Control显示图像 - vipjy2008 - 乐观开朗的小孩
 MFC用Picture Control显示图像 - vipjy2008 - 乐观开朗的小孩
 
拖上去后, 可做相关属性的修改. 如可以其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
  1. void MatToCImage( Mat &mat, CImage &cImage)  
  2. {  
  3.     //create new CImage  
  4.     int width    = mat.cols;  
  5.     int height   = mat.rows;  
  6.     int channels = mat.channels();  
  7.   
  8.     cImage.Destroy(); //clear  
  9.     cImage.Create(width,   
  10.         height, //positive: left-bottom-up   or negative: left-top-down  
  11.         8*channels ); //numbers of bits per pixel  
  12.   
  13.     //copy values  
  14.     uchar* ps;  
  15.     uchar* pimg = (uchar*)cImage.GetBits(); //A pointer to the bitmap buffer  
  16.           
  17.     //The pitch is the distance, in bytes. represent the beginning of   
  18.     // one bitmap line and the beginning of the next bitmap line  
  19.     int step = cImage.GetPitch();  
  20.   
  21.     for (int i = 0; i < height; ++i)  
  22.     {  
  23.         ps = (mat.ptr<uchar>(i));  
  24.         for ( int j = 0; j < width; ++j )  
  25.         {  
  26.             if ( channels == 1 ) //gray  
  27.             {  
  28.                 *(pimg + i*step + j) = ps[j];  
  29.             }  
  30.             else if ( channels == 3 ) //color  
  31.             {  
  32.                 for (int k = 0 ; k < 3; ++k )  
  33.                 {  
  34.                     *(pimg + i*step + j*3 + k ) = ps[j*3 + k];  
  35.                 }             
  36.             }  
  37.         }     
  38.     }  
  39.   
转自:http://vipjy2008.blog.163.com/blog/static/372087672014026314586/

http://blog.csdn.net/merlin_q/article/details/7041040/


0 0
原创粉丝点击