-

- 加为好友
- 发送私信
- 在线聊天
-
guozhuyz
- 等级:

- 可用分等级:
- 总技术分:
- 总技术分排名:
- 揭贴率:
|
| 发表于:2008-04-26 17:37:09 楼主 |
我想用visual c++实现图像的读入,和显示,并且当按下pageDown PageUp时能够分别显示下一幅图像和上一幅图像. ////////////////////////////////////// //readraw.h #include <afxwin.h> class CMyApp:public CWinApp { public: virtual BOOL InitInstance(); }; class CMainWindow:public CWnd { public: int fnum; CString m_nFullName; void OnReadImage(int); CString m_nPath; CString m_nFileTitle; BYTE *image; int fIndex; //索引 CMainWindow(); protected: HDC m_hMemDC; //与客户区兼容的内存DC句柄 int m_nWidth; //raw 图像的宽 int m_nHeight; //raw 高 protected: virtual void PostNcDestroy(); afx_msg BOOL OnCreate(LPCREATESTRUCT); afx_msg void OnKeyDown(UINT,UINT, UINT); afx_msg void OnPaint(); afx_msg void OnDrawImage(HDC,BYTE*); afx_msg void OnDestroy(); afx_msg void OnFileOpen(); afx_msg void OnFileSave(); // afx_msg void OnKeyDown(UINT,UINT, UINT); afx_msg void OnDraw(); DECLARE_MESSAGE_MAP() }; ////////////////////////////////////////////// //ImageProc.cpp #include <afxdlgs.h> #include "resource.h" #include "ReadRaw.h" CMyApp theApp; BOOL CMyApp::InitInstance() { m_pMainWnd=new CMainWindow; m_pMainWnd->ShowWindow(m_nCmdShow); return TRUE; } CMainWindow::CMainWindow() { LPCTSTR lpszClassName = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW, LoadCursor(NULL,IDC_ARROW),(HBRUSH)(COLOR_WINDOW+1),theApp.LoadIcon(IDI_MAIN)); CreateEx(NULL,lpszClassName,"raw文件浏览器", WS_OVERLAPPED|WS_SYSMENU|WS_CAPTION|WS_MINIMIZEBOX, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL); } BEGIN_MESSAGE_MAP(CMainWindow,CWnd) ON_WM_CREATE() ON_WM_PAINT() ON_COMMAND(FILE_OPEN,OnFileOpen) END_MESSAGE_MAP() void CMainWindow::PostNcDestroy() { delete this; } BOOL CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct) { ::SetMenu(m_hWnd,LoadMenu(theApp.m_hInstance,(LPCTSTR)IDR_MAIN)); CClientDC dc(this); //初始化内存DC m_hMemDC = CreateCompatibleDC(dc); m_nHeight = 0; m_nWidth = 0; fIndex = 0; image=NULL; return TRUE; } void CMainWindow::OnPaint() { CPaintDC dc(this); //复制内存中的DC中的图像到客户区 BitBlt(dc,0,0,m_nWidth,m_nHeight,m_hMemDC,0,0,SRCCOPY); OnDrawImage(dc,image); } void CMainWindow::OnDestroy() { DeleteDC(m_hMemDC);} void CMainWindow::OnFileOpen() { // TODO: Add your command handler code here CFileDialog MyFDlg(TRUE, NULL,NULL, OFN_HIDEREADONLY, NULL,NULL ); MyFDlg.m_ofn.lpstrFilter = "(*.lst;*.raw)\0*.lst;*.raw\0"; MyFDlg.m_ofn.lpstrInitialDir = " E:\\学习\\courseware\\数字图像试验\\Experiment1 -3\\"; if (IDOK == MyFDlg.DoModal()) { m_nPath=MyFDlg.GetPathName(); //CString fnstr; m_nFullName=MyFDlg.GetFileName(); m_nFileTitle = MyFDlg.GetFileTitle(); if (m_nPath.Right(3) == "lst") { MessageBox("can not open the lst"); } else // { OnReadImage(fIndex); } } } void CMainWindow::OnReadImage(int fIndex) { FILE *fpImg; fpImg = fopen(m_nFullName, "rb"); if( fpImg==0 ) { AfxMessageBox( "Cannot open the list file", MB_OK, 0 ); return; } fread( &m_nWidth, sizeof(int), 1, fpImg); fread( &m_nHeight, sizeof(int), 1, fpImg); // BYTE *image; if(image) delete image; image = new BYTE[m_nWidth*m_nHeight]; fread(image,sizeof(BYTE),m_nWidth*m_nHeight,fpImg); ::InvalidateRect(m_hWnd,NULL,TRUE); fclose(fpImg); } void CMainWindow::OnDrawImage(HDC dc,BYTE *image) { int gray; for(int i=0;i <m_nHeight;i++) { for(int j=0;j <m_nWidth;j++) { gray=image[i*m_nWidth+j]; SetPixel(dc,j,i,RGB(gray,gray,gray)); } } } void CMainWindow::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default if( nChar == VK_NEXT) { if( fIndex < fnum ) { fIndex++; OnReadImage( fIndex ); } } if( nChar == VK_PRIOR ) { if( fIndex > 0 ) { fIndex--; OnReadImage( fIndex ); } } } 问题是当我按下PageDowm PageUp时没有反应。请问是什么原因? |
|
|
|
20
修改
删除
举报
引用
回复
| |