如何在MFC窗口显示BMP和JPG图片

view plain
  1. //通过资源画图    
  2.   static   void   DrawBitmap(UINT   bitmap,CDC   *pDC,CRect   rect)    
  3.   {    
  4.       CBitmap   bmp;    
  5.       if   (bmp.LoadBitmap(bitmap))    
  6.       {    
  7.       BITMAP   bmpInfo;    
  8.               bmp.GetBitmap(&bmpInfo);    
  9.               CDC   dcMemory;    
  10.               dcMemory.CreateCompatibleDC(pDC);    
  11.               dcMemory.SelectObject(&bmp);    
  12.       pDC->SetStretchBltMode(HALFTONE);    
  13.       pDC->StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),&dcMemory,0,0,bmpInfo.bmWidth,bmpInfo.bmHeight,SRCCOPY);    
  14.       }    
  15.   }    
  16.      
  17.   //通过BMP文件画图    
  18.   static   void   drawBitmapFormFile(CString   BitmapFilePath,CDC   *pDC,CRect   rect)    
  19.   {    
  20.   HANDLE   filehandle=::LoadImage(NULL,BitmapFilePath,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);    
  21.   if(filehandle!=NULL)    
  22.   {    
  23.   CBitmap   bmp;    
  24.   if(bmp.Attach(filehandle))    
  25.   {    
  26.                             BITMAP   bmpInfo;    
  27.                             bmp.GetBitmap(&bmpInfo);    
  28.                             CDC   dcMemory;    
  29.                             dcMemory.CreateCompatibleDC(pDC);    
  30.                             dcMemory.SelectObject(&bmp);    
  31.                             pDC->SetStretchBltMode(HALFTONE);    
  32.                             pDC->StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),&dcMemory,0,0,bmpInfo.bmWidth,bmpInfo.bmHeight,SRCCOPY);    
  33.     bmp.Detach();    
  34.   }    
  35.   }    
  36.   }  
view plain
  1. BOOL CShowBitmapView::ShowJpgGif(CDC* pDC,CString strPath, int x, int y)//读取文件信息方式打开JPG等图片文件  
  2. {  
  3.     IStream *pStm;    
  4.     CFileStatus fstatus;    
  5.     CFile file;    
  6.     LONG cb;    
  7.     //打开文件并检测文件的有效性  
  8.     if (file.Open(strPath,CFile::modeRead)&&  
  9.         file.GetStatus(strPath,fstatus)&&   
  10.         ((cb = fstatus.m_size) != -1))    
  11.     {    
  12.         HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);    
  13.         LPVOID pvData = NULL;    
  14.         if (hGlobal != NULL)    
  15.         {    
  16.             pvData = GlobalLock(hGlobal);  
  17.             if (pvData != NULL)    
  18.             {    
  19.                 //file.ReadHuge(pvData, cb);  //6.0中可能是用这个函数  
  20.                 file.Read(pvData, cb);  //VC2005.NET中用这个函数  
  21.                 GlobalUnlock(hGlobal);    
  22.                 CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);    
  23.             }   
  24.         }   
  25.     }  
  26.     else  
  27.     {  
  28.         return false;  
  29.     } //打开文件结束  
  30.     //显示JPEG和GIF格式的图片,GIF只能显示一帧,还不能显示动画,  
  31.     //要显示动画GIF请使用ACTIVE控件。  
  32.     IPicture *pPic;   
  33.     //load image from file stream  
  34.     if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))   
  35.     {   
  36.         OLE_XSIZE_HIMETRIC hmWidth;    
  37.         OLE_YSIZE_HIMETRIC hmHeight;    
  38.         pPic->get_Width(&hmWidth);    
  39.         pPic->get_Height(&hmHeight);    
  40.         double fX,fY;    
  41.         //get image height and width  
  42.         fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);    
  43.         fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);    
  44.         //use render function display image  
  45.         if(FAILED(pPic->Render(*pDC,x,y,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))    
  46.         {  
  47.             pPic->Release();  
  48.             return false;  
  49.         }  
  50.         pPic->Release();    
  51.     }    
  52.     else    
  53.     {  
  54.         return false;    
  55.     }  
  56.     return true;  


你可能感兴趣的:(.net,image,null,mfc)