MFC利用halcon以及cimage在picture control控件上面显示图片
本人为利用halcon做软件开发的新手,提供在picture control控件上面显示图片的两种简单方法
一、利用cimage
程序如下:
char* pcsun = "图片文件(*.bmp *.png *.jpg)|*.bmp;*.png;*jpg|All Files (*.*)|*.*||";
CFileDialog OpenDialog(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,
_T("Image files(*.png; *.jpg)|*.png;*.jpg|All files(*.*)|*.*||"), NULL);
if (OpenDialog.DoModal() == IDCANCEL) return; //返回带文件名的路径,并传递给编辑控件变量
CString str = OpenDialog.GetPathName();
char* ch = (char*)LPCTSTR(str);
CRect rect;
CImage image;
image.Load(str);
GetDlgItem(IDC_PIC1)->GetWindowRect(&rect); //将客户区选中到控件表示的矩形区域内
CWnd *pWnd = NULL;
pWnd = GetDlgItem(IDC_PIC1);//获取控件句柄
pWnd->GetClientRect(&rect);//获取句柄指向控件区域的大小
CDC *pDc = NULL;
pDc = pWnd->GetDC();//获取picture的DC
image.Draw(pDc->m_hDC, rect);//将图片绘制到picture表示的区域内
ReleaseDC(pDc);
二、利用halcon
程序如下:
HObject m_hoImage;
HTuple width, height;
ReadImage(&m_hoImage, ch);
GetImageSize(m_hoImage, &height, &width);
CRect rtWindow;
HWND hImgWnd = GetDlgItem(IDC_PIC1)->m_hWnd;
GetDlgItem(IDC_PIC1)->GetClientRect(&rtWindow);
HTuple m_htWindow;
OpenWindow(rtWindow.left, rtWindow.top, rtWindow.Width(), rtWindow.Height(), (Hlong)hImgWnd, "visible", "", &m_htWindow);
SetPart(m_htWindow, 0, 0, width, height);//
DispObj(m_hoImage, m_htWindow);
以上两种方法均可实现图片的读取及其显示,效果图如下: