ShowBitmapView.cpp
上传用户:turtfrt
上传日期:2022-06-23
资源大小:36k
文件大小:6k
源码类别:

图片显示

开发平台:

Visual C++

  1. // ShowBitmapView.cpp : implementation of the CShowBitmapView class
  2. //
  3. #include "stdafx.h"
  4. #include "ShowBitmap.h"
  5. #include "ShowBitmapDoc.h"
  6. #include "ShowBitmapView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CShowBitmapView
  14. IMPLEMENT_DYNCREATE(CShowBitmapView, CView)
  15. BEGIN_MESSAGE_MAP(CShowBitmapView, CView)
  16. //{{AFX_MSG_MAP(CShowBitmapView)
  17. ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  18. //}}AFX_MSG_MAP
  19. // Standard printing commands
  20. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  21. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  22. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  23. END_MESSAGE_MAP()
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CShowBitmapView construction/destruction
  26. // CShowBitmapView 构造/析构
  27. CShowBitmapView::CShowBitmapView()
  28. {
  29. // TODO: add construction code here
  30. state=0;
  31. }
  32. CShowBitmapView::~CShowBitmapView()
  33. {
  34. }
  35. BOOL CShowBitmapView::PreCreateWindow(CREATESTRUCT& cs)
  36. {
  37. // TODO: Modify the Window class or styles here by modifying
  38. //  the CREATESTRUCT cs
  39. return CView::PreCreateWindow(cs);
  40. }
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CShowBitmapView drawing
  43. void CShowBitmapView::OnDraw(CDC* pDC)
  44. {
  45. CShowBitmapDoc* pDoc = GetDocument();
  46. ASSERT_VALID(pDoc);
  47. // TODO: add draw code for native data here
  48.    if (!pDoc)
  49.         return;
  50.     if (state==1)
  51.      {
  52.         if (0 == extname.Compare(_T("bmp")))
  53.          {
  54.              CDC dcImage;
  55.             if (!dcImage.CreateCompatibleDC(pDC))
  56.              {
  57.                 return;
  58.              }
  59.              BITMAP bm;
  60.              m_bitmap.GetBitmap(&bm);
  61.              dcImage.SelectObject(&m_bitmap);
  62.              pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&dcImage,0,0,SRCCOPY);
  63.          }
  64.         else
  65.          {
  66.             if (0 == extname.Compare(_T("jpg")))
  67.              {
  68.                  ShowJpgGif(pDC,BmpName,1,1);
  69.              }
  70.          }
  71.      }
  72. }
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CShowBitmapView printing
  75. BOOL CShowBitmapView::OnPreparePrinting(CPrintInfo* pInfo)
  76. {
  77. // default preparation
  78. return DoPreparePrinting(pInfo);
  79. }
  80. void CShowBitmapView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  81. {
  82. // TODO: add extra initialization before printing
  83. }
  84. void CShowBitmapView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  85. {
  86. // TODO: add cleanup after printing
  87. }
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CShowBitmapView diagnostics
  90. #ifdef _DEBUG
  91. void CShowBitmapView::AssertValid() const
  92. {
  93. CView::AssertValid();
  94. }
  95. void CShowBitmapView::Dump(CDumpContext& dc) const
  96. {
  97. CView::Dump(dc);
  98. }
  99. CShowBitmapDoc* CShowBitmapView::GetDocument() // non-debug version is inline
  100. {
  101. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CShowBitmapDoc)));
  102. return (CShowBitmapDoc*)m_pDocument;
  103. }
  104. #endif //_DEBUG
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CShowBitmapView message handlers
  107. BOOL CShowBitmapView::ShowJpgGif(CDC* pDC,CString strPath, int x, int y)
  108. {
  109.     IStream *pStm;  
  110.      CFileStatus fstatus;  
  111.      CFile file;  
  112.      LONG cb;  
  113.     //打开文件并检测文件的有效性
  114.     if (file.Open(strPath,CFile::modeRead)&&
  115.          file.GetStatus(strPath,fstatus)&& 
  116.          ((cb = fstatus.m_size) != -1))  
  117.      {  
  118.          HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);  
  119.          LPVOID pvData = NULL;  
  120.         if (hGlobal != NULL)  
  121.          {  
  122.              pvData = GlobalLock(hGlobal);
  123.             if (pvData != NULL)  
  124.              {  
  125.                 //file.ReadHuge(pvData, cb);  //6.0中可能是用这个函数
  126.                  file.Read(pvData, cb);  //VC2005.NET中用这个函数
  127.                  GlobalUnlock(hGlobal);  
  128.                  CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);  
  129.              } 
  130.          } 
  131.      }
  132.     else
  133.      {
  134.         return false;
  135.      } //打开文件结束
  136.     //显示JPEG和GIF格式的图片,GIF只能显示一帧,还不能显示动画,
  137.     //要显示动画GIF请使用ACTIVE控件。
  138.      IPicture *pPic; 
  139.     //load image from file stream
  140.     if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic))) 
  141.      { 
  142.          OLE_XSIZE_HIMETRIC hmWidth;  
  143.          OLE_YSIZE_HIMETRIC hmHeight;  
  144.          pPic->get_Width(&hmWidth);  
  145.          pPic->get_Height(&hmHeight);  
  146.         double fX,fY;  
  147.         //get image height and width
  148.          fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);  
  149.          fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);  
  150.         //use render function display image
  151.         if(FAILED(pPic->Render(*pDC,x,y,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))  
  152.          {
  153.              pPic->Release();
  154.             return false;
  155.          }
  156.          pPic->Release();  
  157.      }  
  158.     else  
  159.      {
  160.         return false;  
  161.      }
  162.     return true;
  163. }
  164. void CShowBitmapView::ShowBitmap(CString BmpName)
  165. {
  166.     if (state==0)
  167.      {            
  168.          HBITMAP hBitmap=(HBITMAP)LoadImage(NULL,BmpName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
  169.          m_bitmap.Detach();
  170.          m_bitmap.Attach(hBitmap);
  171.          state=1;
  172.          Invalidate();
  173.      }
  174. }
  175. void CShowBitmapView::OnFileOpen() 
  176. {
  177. // TODO: Add your command handler code here
  178.     //CFileDialog dlg(TRUE,_T("BMP"),_T("*.BMP"),OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("位图文件(*.BMP;*.jpg)|*.BMP;*.jpg|"));   //这行代码可以打开BMP格式的图片
  179.      CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("位图文件(*.BMP)|*.BMP|jpg文件(*.jpg)|*.jpg||")); //这行代码可以打开BMP和JPG格式的图片
  180. //gif格式图片的打开在这就不列出来了,留给各位自己尝试吧,对于gif格式图片的显示图片函数和jpg格式图片是同一个函数。
  181.      if (IDOK==dlg.DoModal())
  182.      {
  183.          state=0; 
  184.          BmpName.Format(_T("%s"),dlg.GetPathName());
  185.          extname = dlg.GetFileExt();             //返回选定文件的扩展文件名
  186.          extname.MakeLower();
  187.          if (0==extname.Compare(_T("bmp")))
  188.          {
  189.              ShowBitmap(BmpName);
  190.          }
  191.          else
  192.          {
  193.              state=1;
  194.          }
  195.         Invalidate();
  196.      }
  197. }