mfc- Picture control顯示圖片

來源:互聯網
上載者:User

 

一、放一個Pictrue控制項屬性Type設為Owner Draw
調整大小
二、加一個按鈕控制項用於處理圖片載入,添加事件

三、按鈕單擊事件
 char* m_JPGBuf ;
 int m_JPGLen;//標頭檔中定義

void Cbmlb::OnBnClickedBtnUplogpic()
{
 CString PicPath;
 CFileDialog fdlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"JPEG影像檔|*.jpg|BMP影像檔|*.bmp||");
 if (fdlg.DoModal() == IDOK)
 { 
  PicPath = fdlg.GetPathName();
  CFile file;
  if(file.Open(PicPath,CFile::modeRead) && ((m_JPGLen = file.GetLength()) != -1))
  {
   m_JPGBuf = (char *)malloc(m_JPGLen);
   file.Read(m_JPGBuf, m_JPGLen);
   file.Close();
  }

  DisplayJPG(m_JPGBuf,m_JPGLen);
 }
 ((CStatic*)GetDlgItem(IDC_EDIT_PicPath))->SetWindowText(PicPath);//編輯框可以顯示圖片路經

四、編輯圖片處理方法

BOOL Cbmlb::DisplayJPG(char* JPGBuf ,int JPGLen)
{
 m_JPGBuf = JPGBuf;
 m_JPGLen = JPGLen;
 IStream *pStm;
 LPVOID pvData;
 HGLOBAL hGlobal;
 CRect   m_DisplayRect,m_winRect;
 CDC* m_pDisplayCDC;
 m_pDisplayCDC = m_PersonPicBtn.GetDC();
 m_PersonPicBtn.GetClientRect(&m_DisplayRect);//擷取畫單張圖的RECT

 double m_HORZRES = (double)m_pDisplayCDC->GetDeviceCaps(HORZRES);
 double m_VERTSIZE = (double)m_pDisplayCDC->GetDeviceCaps(VERTSIZE);

 if(JPGBuf == NULL)
 {
  CBrush br(RGB(0,0,0));
  m_pDisplayCDC->FillRect(&m_DisplayRect, &br);
  return FALSE;
 }

 char *pJPGBuf = JPGBuf;
 int iJPGLen = JPGLen;
 hGlobal = GlobalAlloc(GMEM_MOVEABLE, iJPGLen);
 pvData = NULL;
 if(hGlobal != NULL)
 {
  pvData = GlobalLock(hGlobal);
  if(pvData != NULL)
  {
   memcpy(pvData,pJPGBuf,iJPGLen);
   GlobalUnlock(hGlobal);
   CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
  }
 }

 //開啟檔案結束
 //顯示JPEG和GIF格式的圖片,GIF只能顯示一幀,還不能顯示動畫,
 //要顯示動畫GIF請使用ACTIVE控//件。
 IPicture *pPic;     //load image from file stream
 if(SUCCEEDED(OleLoadPicture(pStm,iJPGLen,TRUE,IID_IPicture,(LPVOID*)&pPic)))
 {
  OLE_XSIZE_HIMETRIC hmWidth;
  OLE_YSIZE_HIMETRIC hmHeight;
  pPic->get_Width(&hmWidth);
  pPic->get_Height(&hmHeight);      
  double fX,fY;
  //get image height and width       
  fX = m_HORZRES*(double)hmWidth/(m_HORZRES*100.0);
  fY = m_VERTSIZE*(double)hmHeight/(m_VERTSIZE*100.0);
  //use render function display image 
  if(FAILED(pPic->Render(*m_pDisplayCDC,0,0,m_DisplayRect.Width(),m_DisplayRect.Height(),0,hmHeight,hmWidth,-hmHeight,NULL)))
  {           
   pPic->Release();
   pStm->Release();
   if(hGlobal != NULL)
    GlobalFree(hGlobal);
   ReleaseDC(m_pDisplayCDC);
   return FALSE;     
  }
  pPic->Release(); 
  pStm->Release();
 }
 else
 {
  pStm->Release();
  if(hGlobal != NULL)
   GlobalFree(hGlobal);
  ReleaseDC(m_pDisplayCDC);
  return FALSE;
 }
 if(hGlobal != NULL)
 {
  GlobalFree(hGlobal);
 }

 ReleaseDC(m_pDisplayCDC);

 return TRUE; 
}
五、加ONPAIN方法
void Cbmlb::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 // TODO: 在此處添加訊息處理常式代碼
 // 不為繪圖訊息調用 CDialog::OnPaint()
 DisplayJPG(m_JPGBuf,m_JPGLen);

}

歡迎加入vc技術交流群:320307654

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.