MFC display JPG picture _c

Source: Internet
Author: User

: http://blog.csdn.net/crearo/article/details/1328974

-----------------------------------------------------------------------------------------------

Screenshot of Effect:


--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------

The main code is as follows:

Method Description: Display jpg and GIF, BMP picture//Parameter description: CDC * pDC Device Environment object//Parameter description: CSTR ing strpath picture Path//Parameter description: int x to display x position//Parameter description: int y to display the Y position//return value: BOO L successfully returns TRUE, otherwise returns false//************************************ BOOL Cshowjpgdlg::showjpggif (cdc* pdc,cstring s  
    Trpath, int x, int y) {cfilestatus fstatus;  
    CFile file;  

    ULONGLONG CB; Open the file and detect the validity of the file if (!file).
     Open (Strpath,cfile::moderead)) {return FALSE; } if (!file.
     GetStatus (Strpath,fstatus)) {return FALSE;
     if (CB =fstatus.m_size) <=0) {return FALSE;  
      ///Allocate memory space according to file size, remember to release memory hglobal hglobal = GlobalAlloc (gmem_moveable, (unsigned int) CB);
      if (hglobal== NULL) {return FALSE;  
      //Lock the memory space just allocated lpvoid pvData = NULL;
      PvData = GlobalLock (hglobal);  
  if (PvData = NULL)    {GlobalFree (HGLOBAL);
      Remember to free memory return FALSE;  
      //Put the file in the stream IStream *pstm; File.  
      Read (PvData, (unsigned int) CB);  
      GlobalUnlock (HGLOBAL);  
 
    CreateStreamOnHGlobal (Hglobal, TRUE, &pstm);
    Load pictures from the stream//display JPEG and GIF format pictures, GIF can only display one frame, can not display animation,///To display an animated GIF, use the active control. 
    IPicture *ppic; if (OleLoadPicture (PStm, (LONG) fstatus.m_size,true,iid_ipicture, (lpvoid*) &ppic)!=s_ok) {GlobalFree (HGloba  L);
    Remember to free memory return FALSE;  
    //Get the image wide and high, note that the width and height here is not the resolution of the image Ole_xsize_himetric Hmwidth;  
    Ole_ysize_himetric Hmheight;  
    Ppic->get_width (&hmwidth);  

    Ppic->get_height (&hmheight); Converts the image width and height units to pixel units//#define HIMETRIC_PER_INCH 2540//int npicwidth = MulDiv (Hmwidth, GetDeviceCaps (GetDC ()->m
   _HDC, logpixelsx), 2540);

   
    int npicheight = MulDiv (Hmheight, GetDeviceCaps (GetDC ()-&GT;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 OFFSE T in source picture//Ole_ypos_himetric ysrc,//vertical offset into source picture//Ole_xsize_himetri C cxsrc,//amount to copy horizontally in source picture//Ole_ysize_himetric cysrc,//amount to copy vertic
    Ally 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);
    Remember to free memory return false; 
    } ppic->release ();  GlobalFree (HGLOBAL);
Remember to free memory return true; //************************************//Method Description: Display jpg and GIF, BMP picture//Parameter description: CDC * pDC Device Environment object//Parameter description: C      String strpath the picture path//Parameter description to display: int x to display the x position//Parameter description: int y to display the Y position//return value:  BOOL returns true successfully, otherwise returns 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;  
    //Get the image wide and high, note that the width and height here is not the resolution of the image Ole_xsize_himetric Hmwidth;  
    Ole_ysize_himetric Hmheight;  
    Ppic->get_width (&hmwidth);  

    Ppic->get_height (&hmheight); //Convert image width and height units to pixel units//#define HIMETRIC_PER_INCH 2540//int npicwidth = MulDiv (Hmwidth, GetDeviceCaps (GetDC ()->m_
   HDC, logpixelsx), 2540);

   int npicheight = MulDiv (Hmheight, GetDeviceCaps (GetDC ()-&GT;M_HDC, logpixelsy), 2540);
   Gets the width and height of the Display picture window 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->Releas
       E ();
   return false; 
   
   /Remember to release the resource, or it will cause a memory leak ppic->release ();
return true; //************************************//Method Description: Browse picture//return value: void//************************************ void Cshowjpgdlg::onbnclickedbtnbrowse () {//Todo:add your control notification handler code here CFileDialog dlg (TRU E, "JPG", "*.jpg", ofn_hidereadonly| Ofn_overwriteprompt, "JPEG file (*.jpg) |*.jpg| GIF file (*.gif) |*.gif|bmp file (*.bmp) |*.BMP| ", NULL); if (dlg. DoModal () ==idok) {setdlgitemtext, Idc_txt_path,dlg.

        GetPathName ()); Sets the style of a static control so that it can use a bitmap and displays the bitmap centered ((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); Remember to release the resource, or it will cause a memory leak}}

--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------

Example Engineering (VS2010) Download address: http://download.csdn.net/detail/friendan/7709595

-----------------------------------------------------------------------------------------------------

Your very satisfaction is my pursuit of the purpose.

A little advice from you is my motivation to follow.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.