MFC单文档显示图像我知道的两种方法

来源:互联网 发布:域名授权源码 编辑:程序博客网 时间:2024/05/17 05:58

工程建好后,右击文档类选择建立类向导,选择虚函数中的OnOpenDocument,编辑

在文档类里面添加Mat img;和string path;

同时需要添加头文件#include<opencv2/opencv.hpp>

BOOL CsplitDoc::OnOpenDocument(LPCTSTR lpszPathName){if (!CDocument::OnOpenDocument(lpszPathName))return FALSE;// TODO:  Add your specialized creation code herepath = CT2A(lpszPathName);// TODO:  Add your specialized creation code hereimg = cv::imread(path);if (img.empty()){MessageBox(NULL, lpszPathName, _T("did't load the pic"), MB_OK);}return TRUE;}

方法一,进入视图类,编辑OnDraw函数

同时要包含头文件#include<opencv2/opencv.hpp>

void CsplitView::OnDraw(CDC* pDC){CsplitDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);if (!pDoc)return;cv::Mat newImage;if (pDoc->img.channels() == 1){cv::cvtColor(pDoc->img, newImage, CV_GRAY2BGRA);}else if (pDoc->img.channels() == 3){cv::cvtColor(pDoc->img, newImage, CV_BGR2BGRA);}else{newImage = pDoc->img;}// TODO: add draw code for native data hereif (!pDoc->img.empty()){CImage  image;int cx, cy;CRect   rect;std::wstring str;std::string path1 = pDoc->path;StringToWString(path1, str);image.Load((LPCTSTR)str.c_str());CImage image2 = image;//获取图片的宽 高  SetRect(rect, 0, 0, newImage.cols, newImage.rows);pDC = GetDC();//获取picture control的DCimage.Draw(pDC->m_hDC, rect);ReleaseDC(pDC);}}

方法一中用到的StringToWString函数如下

BOOL StringToWString(const std::string &str,std::wstring &wstr) {         int nLen = (int)str.length();         wstr.resize(nLen,L' ');      int nResult = MultiByteToWideChar(CP_ACP,0,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen);      if (nResult == 0)     {         return FALSE;     }      return TRUE; }
这个函数使用的时候要位置要放在OnDraw函数的前面即可;

方法二:1.打开视图类里面的OnDraw函数

void CAgainTestView::OnDraw(CDC* pDC){CAgainTestDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);if (!pDoc)return;// TODO: 在此处为本机数据添加绘制代码  cv::Mat newImage;if (pDoc->img.channels() == 1){cv::cvtColor(pDoc->img, newImage, CV_GRAY2BGRA);}else if (pDoc->img.channels() == 3){cv::cvtColor(pDoc->img, newImage, CV_BGR2BGRA);}else{newImage = pDoc->img;}Gdiplus::Bitmap bitmap(newImage.cols, newImage.rows, newImage.step1(), PixelFormat32bppARGB, newImage.data);Gdiplus::Graphics graphics(pDC->GetSafeHdc());graphics.DrawImage(&bitmap, 0, 0);// TODO: add draw code for native data hereGdiplus::Bitmap bitmap1(newImage.cols, newImage.rows, newImage.step1(), PixelFormat32bppARGB, newImage.data);graphics.DrawImage(&bitmap1, newImage.cols, 0);
CSize scroll_size;scroll_size.cx = newImage.cols * 2;scroll_size.cy = newImage.rows;SetScrollSizes(MM_TEXT, scroll_size);ReleaseDC(pDC);}
然后进入APP头文件中添加
#include<gdiplus.h>using namespace Gdiplus;
class CAgainTestApp : public CWinAppEx{public:CAgainTestApp();GdiplusStartupInput gdiplusStartupInput;ULONG_PTR gdiplusToken;
然后进入APP的.cpp中添加如下

// The one and only window has been initialized, so show and update itm_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);return TRUE;}int CAgainTestApp::ExitInstance(){//TODO: handle additional resources you may have addedAfxOleTerm(FALSE);GdiplusShutdown(gdiplusToken);return CWinAppEx::ExitInstance();}





阅读全文
4 0