VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 397|回复: 1

mfc代码创建图片控件

[复制链接]
89_avatar_middle
最佳答案
0 
在线会员 发表于 2022-3-22 10:37:27 | 显示全部楼层 |阅读模式
mfc如何用代码创建图片控件?
我用以下代码无法显示图片
  1.        
  2.           CStatic* pstaDis = new CStatic;
  3.         pstaDis->Create(_T(""), WS_CHILD | WS_VISIBLE | SS_BITMAP | SS_CENTERIMAGE|WS_BORDER, CRect(50, 50, 300, 300), this, 12345);
复制代码


显示代码如下

  1. void CsoftwareDlg::DrawPicToHdc(Mat p, CWnd* pWnd)
  2. {//显示函数

  3.         Mat p1; //opencv中的图像
  4.         CImage img;
  5.         CRect rect; //定义矩形类

  6.         //CWnd* pWnd = GetDlgItem(IDC);                                                                        //获取控件句柄  
  7.         pWnd->GetClientRect(&rect);                                                                                //获取句柄指向控件区域的大小  
  8.         CDC* pDc = pWnd->GetDC();                                                                                //获取picture控件的DC  
  9.         int win_w = rect.Width(), win_h = rect.Height();                                //获取控件窗口宽高
  10.         pDc->SetStretchBltMode(COLORONCOLOR);

  11.         resize(p, p1, Size(win_w, win_h));                                                                //调整图片大小。

  12.         MatToCImage(p1, img);                                                                                        //将mat转变为cimage


  13.         img.Draw(pDc->m_hDC, 0, 0, win_w, win_h, 0, 0, win_w, win_h);        //画出图片
  14.         ReleaseDC(pDc);
  15. }
复制代码

  1. void MatToCImage(Mat& mat, CImage& cimage)
  2. {
  3.         if (0 == mat.total())
  4.         {
  5.                 return;
  6.         }


  7.         int nChannels = mat.channels();
  8.         if ((1 != nChannels) && (3 != nChannels))
  9.         {
  10.                 return;
  11.         }
  12.         int nWidth = mat.cols;
  13.         int nHeight = mat.rows;


  14.         //重建cimage
  15.         cimage.Destroy();
  16.         cimage.Create(nWidth, nHeight, 8 * nChannels);

  17.         //拷贝数据
  18.         uchar* pucRow;                                                                        //指向数据区的行指针
  19.         uchar* pucImage = (uchar*)cimage.GetBits();                //指向数据区的指针
  20.         int nStep = cimage.GetPitch();                                        //每行的字节数,注意这个返回值有正有负


  21.         if (1 == nChannels)                                                                //对于单通道的图像需要初始化调色板
  22.         {
  23.                 RGBQUAD* rgbquadColorTable;
  24.                 int nMaxColors = 256;
  25.                 rgbquadColorTable = new RGBQUAD[nMaxColors];
  26.                 cimage.GetColorTable(0, nMaxColors, rgbquadColorTable);
  27.                 for (int nColor = 0; nColor < nMaxColors; nColor++)
  28.                 {
  29.                         rgbquadColorTable[nColor].rgbBlue = (uchar)nColor;
  30.                         rgbquadColorTable[nColor].rgbGreen = (uchar)nColor;
  31.                         rgbquadColorTable[nColor].rgbRed = (uchar)nColor;
  32.                 }
  33.                 cimage.SetColorTable(0, nMaxColors, rgbquadColorTable);
  34.                 delete[]rgbquadColorTable;
  35.         }


  36.         for (int nRow = 0; nRow < nHeight; nRow++)
  37.         {
  38.                 pucRow = (mat.ptr<uchar>(nRow));
  39.                 for (int nCol = 0; nCol < nWidth; nCol++)
  40.                 {
  41.                         if (1 == nChannels)
  42.                         {
  43.                                 *(pucImage + nRow * nStep + nCol) = pucRow[nCol];
  44.                         }
  45.                         else if (3 == nChannels)
  46.                         {
  47.                                 for (int nCha = 0; nCha < 3; nCha++)
  48.                                 {
  49.                                         *(pucImage + nRow * nStep + nCol * 3 + nCha) = pucRow[nCol * 3 + nCha];
  50.                                 }
  51.                         }
  52.                 }
  53.         }
  54. }
复制代码





上一篇:交叉编译问题
下一篇:vs2019中自定义strcat函数为何没有手动分配内存也可以成功运行
20_avatar_middle
最佳答案
0 
在线会员 发表于 2022-3-23 21:28:18 | 显示全部楼层
  1.         CImage mImgPTRTEST;
  2.         HRESULT hr_PTRTEST = mImgPTRTEST.Load(_T("C:\\Users\\Administrator\\Desktop\\MFCText\\res\\123.jpg"));   //图片路径(别忘加上后缀)
  3.         if (hr_PTRTEST == S_OK)
  4.         {
  5.                 HBITMAP hBingPTRTEST = mImgPTRTEST.Detach();
  6.                 m_PTRTEST.SetBitmap(hBingPTRTEST);
  7.         }
复制代码


这样就可以了,图片要改成bitmap
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-9-29 18:35

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表