MFC對話框中顯示BMP,JPG圖片


//************************************

// 方法說明:    顯示JPG和GIF、BMP圖片

// 參數說明:    CDC * pDC           設備環境對象

// 參數說明:    CString strPath     要顯示的圖片路徑 

// 參數說明:    int x               要顯示的X位置

// 參數說明:    int y               要顯示的Y位置

// 返回值:      BOOL                成功返回TRUE,否則返回FALSE

//************************************

BOOL CShowJpgDlg::ShowJpgGif(CDC* pDC,CString strPath, int x, int y)

 

{

    CFileStatus fstatus;  

    CFile file;  

    ULONGLONG cb;  

 

    // 打開文件並檢測文件的有效性

     if (!file.Open(strPath,CFile::modeRead))

     {

         return FALSE;

     }

     if (!file.GetStatus(strPath,fstatus))

     {

         return FALSE;

     }

     if ((cb =fstatus.m_size)<=0)

     {

         return FALSE;

     }

 

     // 根據文件大小分配內存空間,記得釋放內存

     HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, (unsigned int)cb);  

      if (hGlobal== NULL) 

      {

          return FALSE;

      }

 

      // 鎖定剛才分配的內存空間

      LPVOID pvData = NULL;  

      pvData = GlobalLock(hGlobal);

      if (pvData == NULL)  

      {  

            GlobalFree(hGlobal);  // 記得釋放內存

            return FALSE;

      } 

 

      // 將文件放到流中

      IStream *pStm;  

      file.Read(pvData,(unsigned int)cb);  

      GlobalUnlock(hGlobal);  

      CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);  

 

    // 從流中加載圖片

    // 顯示JPEG和GIF格式的圖片,GIF只能顯示一幀,還不能顯示動畫,

    // 要顯示動畫GIF請使用ACTIVE控件。

    IPicture *pPic; 

    if(OleLoadPicture(pStm,(LONG)fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)!=S_OK) 

    { 

        GlobalFree(hGlobal);  // 記得釋放內存

        return FALSE;

    }

 

    //獲取圖像寬和高,注意這里的寬和高不是圖像的分辨率

    OLE_XSIZE_HIMETRIC hmWidth;  

    OLE_YSIZE_HIMETRIC hmHeight;  

    pPic->get_Width(&hmWidth);  

    pPic->get_Height(&hmHeight);  

 

    // 將圖像寬度和高度單位轉化為像素單位

   //#define HIMETRIC_PER_INCH 2540

   //int nPicWidth =  MulDiv(hmWidth, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSX),2540);

   //int nPicHeight = MulDiv(hmHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY),2540);

 

   

    //HRESULT Render(

    //    HDC hdc, //Handle of device context on which to render the image

    //    long x,  //Horizontal position of image in hdc

    //    long y,  //Vertical position of image in hdc

    //    long cx, //Horizontal dimension of destination rectangle

    //    long cy, //Vertical dimension of destination rectangle

    //    OLE_XPOS_HIMETRIC xSrc,      //Horizontal offset in source picture

    //    OLE_YPOS_HIMETRIC ySrc,      //Vertical offset in source picture

    //    OLE_XSIZE_HIMETRIC cxSrc,    //Amount to copy horizontally in source picture

    //    OLE_YSIZE_HIMETRIC cySrc,    //Amount to copy vertically in source picture

    //    LPCRECT prcWBounds           //Pointer to position of destination for a metafile hdc

    //    );

 

    //use render function display image

    RECT rtWnd;

    pDC->GetWindow()->GetWindowRect(&rtWnd);

    int iWndWidth=rtWnd.right-rtWnd.left;

    int iWndHeight=rtWnd.bottom-rtWnd.top;

    

    if(FAILED(pPic->Render(*pDC,x,y,iWndWidth,iWndHeight,0,hmHeight,hmWidth,-hmHeight,NULL)))  

    {

        pPic->Release();

        GlobalFree(hGlobal);  // 記得釋放內存

        return false;

    }

 

    pPic->Release(); 

    GlobalFree(hGlobal);  // 記得釋放內存

    return true;

}

 

//************************************

// 方法說明:    顯示JPG和GIF、BMP圖片

// 參數說明:    CDC * pDC           設備環境對象

// 參數說明:    CString strPath     要顯示的圖片路徑 

// 參數說明:    int x               要顯示的X位置

// 參數說明:    int y               要顯示的Y位置

// 返回值:      BOOL                成功返回TRUE,否則返回FALSE

//************************************

BOOL CShowJpgDlg::ShowImage(CDC* pDC,CString strPath, int x, int y)

{

   

    IPicture *pPic=NULL; 

    OleLoadPicturePath(CComBSTR(strPath.GetBuffer()), (LPUNKNOWN)NULL, 0, 0, IID_IPicture,(LPVOID*)&pPic);

    if (NULL==pPic)

    {

        return FALSE;

    }

 

    // 獲取圖像寬和高,注意這里的寬和高不是圖像的分辨率

    OLE_XSIZE_HIMETRIC hmWidth;  

    OLE_YSIZE_HIMETRIC hmHeight;  

    pPic->get_Width(&hmWidth);  

    pPic->get_Height(&hmHeight);  

 

    // 將圖像寬度和高度單位轉化為像素單位

   //#define HIMETRIC_PER_INCH 2540

   //int nPicWidth =  MulDiv(hmWidth, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSX),2540);

   //int nPicHeight = MulDiv(hmHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY),2540);

 

   // 獲取顯示圖片窗口的寬度和高度

   RECT rtWnd;

   pDC->GetWindow()->GetWindowRect(&rtWnd);

   int iWndWidth=rtWnd.right-rtWnd.left;

   int iWndHeight=rtWnd.bottom-rtWnd.top;

 

   if(FAILED(pPic->Render(*pDC,x,y,iWndWidth,iWndHeight,0,hmHeight,hmWidth,-hmHeight,NULL)))  

   {

       pPic->Release();

       return false;

   }

 

   //記得釋放資源,不然會導致內存泄露

   pPic->Release(); 

   

   return true;

}

 

 

//************************************

// 方法說明:    瀏覽圖片

// 返回值:      void

//************************************

void CShowJpgDlg::OnBnClickedBtnBrowse()

{

    // TODO: Add your control notification handler code here

    CFileDialog dlg(TRUE,"jpg","*.jpg", OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, 

        "JPEG文件(*.jpg)|*.jpg|GIF文件(*.gif)|*.gif|bmp文件(*.bmp)|*.bmp|",NULL); 

    if(dlg.DoModal()==IDOK) 

    { 

        SetDlgItemText(IDC_TXT_PATH,dlg.GetPathName());

 

        //設置靜態控件的樣式,使其可以使用位圖,並使位圖顯示居中

        ((CStatic*)GetDlgItem(IDC_STATIC_IMG))-> ModifyStyle(0xF,SS_BITMAP|SS_CENTERIMAGE);

 

CDC *pDC=NULL;

pDC=GetDlgItem(IDC_STATIC_IMG)->GetDC();

        //ShowJpgGif(pDC,dlg.GetPathName(),0,0);

        ShowImage(pDC,dlg.GetPathName(),0,0);

 

ReleaseDC(pDC);// 記得釋放資源,不然會導致內存泄露

    } 

 

}

1、MFC對話框顯示BMP圖片

我們先從簡單的開始吧.先分一個類:


(一) 非動態顯示圖片(即圖片先通過資源管理器載入,有一個固定ID)

(二) 動態載入圖片(即只需要在程序中指定圖片的路徑即可載入)

為方便說明,我們已經建好一個基於對話框的工程,名為Ttest.

對話框類為CTestDlg

(一)    非動態載入圖片.

方法1.先從最簡單的開始,用picture 控件來實現.

步驟:

先在資源里Import一張圖片,ID為IDB_BITMAP2

然后在對話框上添加一個picture控件,右鍵點擊打開屬性,

將type下拉框選擇BITMAP,緊跟着下面就出現一個Image下拉框,

拉開就會看到所有已經載入好的圖片,

選擇你要的圖片.運行程序即可看到.

方法2.通過背景圖

同樣如上,先載入一張圖片,ID為IDB_BITMAP2

TestDlg.h中

CBrush m_brBk;//在public中定義 

TestDlg.cpp中

在初始化函數OnInitDialog()中加入:

BOOL CTestDlg::OnInitDialog()

{
         CDialog::OnInitDialog();

         CBitmap bmp;

         bmp.LoadBitmap(IDB_BITMAP2);

         m_brBk.CreatePatternBrush(&bmp);

         bmp.DeleteObject();

         return TRUE;  // return TRUE  unless you set the focus to a control
}

在打開類向導,找到WM_CTLCOLOR消息,重載得對應函數OnCtlColor(),添加如下:

HBRUSH  CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
         HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

         if (pWnd == this)
         {
                return m_brBk;
         }

        return hbr;
}

(二)    動態載入圖片.

方法3 圖像控件(本例用KoDak 圖像編輯控件)

1.    首先應該保證系統中有這個控件。注意,它不能單獨使用,必須和其他幾個控件(特別是Imgcmn.dll)一同使用。如果沒有,從別的機器上copy過來即可。這幾個文件是Imgadmin.ocx,Imgcmn.dll,Imgedit.ocx,Imgscan.ocx,Imgshl.dll,Imgthumb.ocx,Imgutil.dll,把它們copy到windows/system目錄下,然后用regsvr32.exe將它們分別注冊。 

2.    打開工程,進入資源管理器,在對話框上單擊右鍵,單擊Insert Activex control… 選擇Kodak圖象編輯控件,大小任意。 

3.    在對話框上選中該控件,為其添加變量:m_ctrlPicture。。 

4.    在BOOL CTestDlg::OnInitDialog()添加如下:

BOOL CTestDlg::OnInitDialog()

{
         CDialog::OnInitDialog();

         m_ctrlPicture.SetImage("aa.jpg");  //保證圖像在工程目錄下,也可以寫絕對路徑

         m_ctrlPicture.Display();

         return TRUE;  // return TRUE unless you set the focus to a control

         // EXCEPTION: OCX Property Pages should return FALSE
}

編譯運行就OK了,此種方法的好處就是可能針對多種圖像格式.


方法4 通過CBitmap,HBITMAP,直接用OnPaint()繪制

首先在CTestDlg類中聲明一個變量:   CBitmap  m_bmp;

然后我們在對話框中加入一個picture 標簽,名為IDC_STATIC1

然后:

BOOL CDisplayPic::OnInitDialog() 

{
       CDialog::OnInitDialog();

       if( m_bmp.m_hObject != NULL )//判斷

              m_bmp.DeleteObject();

       /////////載入圖片

       HBITMAP hbmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), 

        "c://aaa.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION|LR_LOADFROMFILE);

       if( hbmp == NULL ) 

           return FALSE;

       ///////////////////////該斷程序用來取得加載的BMP的信息////////////////////////

       m_bmp.Attach( hbmp );

       DIBSECTION ds;

       BITMAPINFOHEADER &bminfo = ds.dsBmih; 

       m_bmp.GetObject( sizeof(ds), &ds );

       int cx=bminfo.biWidth;  //得到圖像寬度

       int cy=bminfo.biHeight; //得到圖像高度

      /////////////////// ////////////////////////////////

      /////////////得到了圖像的寬度和高度后,我們就可以對圖像大小進行適應,即調整控件的大小,讓它正好顯示一張圖片///////////////////////////

       CRect rect;

       GetDlgItem(IDC_STATIC1)->GetWindowRect(&rect);

       ScreenToClient(&rect);

       GetDlgItem(IDC_STATIC1)->MoveWindow(rect.left,rect.top,cx,cy,true);//調整大小

       return TRUE;  // return TRUE unless you set the focus to a control

      // EXCEPTION: OCX Property Pages should return FALSE
}

圖片加載成功了,標簽大小也適應了,下面就是繪制繪制圖像了,打開類向導,重載WM_PAINT消息

void CDisplayPic::OnPaint() 

{
       //////////////以下三種情況任選一種會是不同效果(只能一種存在)///////////

       //CPaintDC dc(this);      //若用此句,得到的是對話框的DC,圖片將被繪制在對話框上.

       CPaintDC dc(GetDlgItem(IDC_STATIC1)); //用此句,得到picture控件的DC,圖像將被繪制在控件上  

        //若用以下兩句,得到的是屏幕的DC,圖片將被繪制在屏幕上/////////////////////////////////////////////////////
      
       //  CDC dc;


       //  dc.m_hDC=::GetDC(NULL); 

       CRect rcclient;

       GetDlgItem(IDC_STATIC1)->GetClientRect(&rcclient);

       CDC memdc;

       memdc.CreateCompatibleDC(&dc);  

       CBitmap bitmap;

       bitmap.CreateCompatibleBitmap(&dc, rcclient.Width(), rcclient.Height());

       memdc.SelectObject( &bitmap );

       CWnd::DefWindowProc(WM_PAINT, (WPARAM)memdc.m_hDC , 0);

       CDC maskdc;

       maskdc.CreateCompatibleDC(&dc);

       CBitmap maskbitmap;

       maskbitmap.CreateBitmap(rcclient.Width(), rcclient.Height(), 1, 1, NULL);

       maskdc.SelectObject( &maskbitmap );

       maskdc.BitBlt( 0, 0, rcclient.Width(), rcclient.Height(), &memdc, 

        rcclient.left, rcclient.top, SRCCOPY);

       CBrush brush;

       brush.CreatePatternBrush(&m_bmp);

       dc.FillRect(rcclient, &brush);    

       dc.BitBlt(rcclient.left, rcclient.top, rcclient.Width(), rcclient.Height(), 

             &memdc, rcclient.left, rcclient.top,SRCPAINT);

       brush.DeleteObject();
       // Do not call CDialog::OnPaint() for painting messages

}
以上四種方法唯有KoDak可以支持多種圖像,其它的只支持BMP

以上轉自:http://blog.csdn.net/cecilia214/article/details/5346302

以下轉自:http://topic.csdn.net/u/20090410/10/ad7a5afe-c906-45ff-a673-f601e2ec05be.html

2、MFC對話框顯示JPEG圖片

放在對話框的OnPaint里

 

 1  CDC *  pDC;   
 2  pDC = GetDC();   
 3  ShowPicture(pDC, " c:\\12.jpg " , 0 , 0 , 200 , 100 );  
 4 
 5 
 6 
 7  #define    HIMETRIC_INCH 2540    
 8  LPPICTURE   gpPicture;   
 9  void  CCDMADlg::ShowPicture(CDC  * pDC, CString m_strBRoute,  int  x,  int  y,  int  width,  int  height) 
10    {    
11          HANDLE   hFile  = CreateFile(m_strBRoute,   GENERIC_READ,    0 ,   NULL,   OPEN_EXISTING,    0 ,   NULL);    
12          _ASSERTE(INVALID_HANDLE_VALUE  !=  hFile);    
13       
14           //    取得文件大小    
15          DWORD   dwFileSize    =    GetFileSize(hFile,   NULL);    
16          _ASSERTE( - 1     !=    dwFileSize);    
17          LPVOID   pvData    =    NULL;    
18           // 根據文件大小分配內存    
19          HGLOBAL   hGlobal    =    GlobalAlloc(GMEM_MOVEABLE,   dwFileSize);    
20          _ASSERTE(NULL    !=    hGlobal);    
21          pvData    =    GlobalLock(hGlobal);    
22          _ASSERTE(NULL    !=    pvData);    
23          DWORD   dwBytesRead    =     0 ;    
24           // 讀取文件並存入全局內存    
25          BOOL   bRead    =    ReadFile(hFile,   pvData,   dwFileSize,    & dwBytesRead,   NULL);    
26          _ASSERTE(FALSE    !=    bRead);    
27          GlobalUnlock(hGlobal);    
28          CloseHandle(hFile);    
29          LPSTREAM   pstm    =    NULL;    
30           //    通過全局內存創建   IStream*   的指針    
31          HRESULT   hr    =    CreateStreamOnHGlobal(hGlobal,   TRUE,    & pstm);    
32          _ASSERTE(SUCCEEDED(hr)    &&    pstm);    
33           // 通過圖形文件創建IPicture   對象    
34           if    (gpPicture)    
35                gpPicture -> Release();    
36          hr    =    OleLoadPicture(pstm,   dwFileSize,   FALSE,   IID_IPicture,   (LPVOID    * ) & gpPicture);    
37          _ASSERTE(SUCCEEDED(hr)    &&    gpPicture);    
38          pstm -> Release();    
39          HDC   hdc;    
40          hdc = pDC -> GetSafeHdc();    
41           if    (gpPicture)    
42          {    
43                 //    取得圖片的寬和高    
44                 long    hmWidth;    
45                 long    hmHeight;    
46                gpPicture -> get_Width( & hmWidth);    
47                gpPicture -> get_Height( & hmHeight);    
48                 // 寬高轉換為象素    
49                 int    nWidth  =    MulDiv(hmWidth,   GetDeviceCaps(hdc,   LOGPIXELSX),   HIMETRIC_INCH);    
50                 int    nHeight  =    MulDiv(hmHeight,   GetDeviceCaps(hdc,   LOGPIXELSY),   HIMETRIC_INCH);    
51                RECT   rc;    
52                GetClientRect( & rc); /* 取得客戶區 */     
53                gpPicture -> Render(hdc,   x,y,   ( int )height * hmWidth / hmHeight,height,    0 ,   hmHeight,   hmWidth,    - hmHeight,    & rc);    
54                 /* 顯示圖片 */     
55          }       
57    } 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM