dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Display image.jpg in MFC

  1. #1
    Guest

    Display image.jpg in MFC

    Help me for disply image.jpg in a dialog box
    Thank tou


  2. #2
    Join Date
    Jun 1999
    Location
    Germany
    Posts
    343

    Re: Display image.jpg in MFC

    Hello,

    here's a code example:
    Create a dialog and use this class.
    Have a look in OnInitDialog -> the filename has to be changed.

    Code:
    #if !defined(AFX_SHOWJPG_H__59AA2FD8_96B7_11D3_B055_00A0C9D7E71C__INCLUDED_)
    #define AFX_SHOWJPG_H__59AA2FD8_96B7_11D3_B055_00A0C9D7E71C__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // ShowJPG.h : header file
    //
    
    /////////////////////////////////////////////////////////////////////////////
    // CShowJPG dialog
    #include <afxctl.h>
    
    
    class CShowJPG : public CDialog
    {
    // Construction
    public:
    	CShowJPG(CWnd* pParent = NULL);   // standard constructor
    
    // Dialog Data
    	//{{AFX_DATA(CShowJPG)
    	enum { IDD = IDD_SHOWJPG };
    		// NOTE: the ClassWizard will add data members here
    	//}}AFX_DATA
    
    
    // Overrides
    	// ClassWizard generated virtual function overrides
    	//{{AFX_VIRTUAL(CShowJPG)
    	protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    	//}}AFX_VIRTUAL
    
    // Implementation
    protected:
    	void SetFile( CString& strPath);
    	CPictureHolder m_Picture;
    	CBitmap m_Bmp;
    	CBitmap *m_pOld;
    	CRect m_BmpRect;
    	void CreateBitmap();
    	CDC m_DC;
    	// Generated message map functions
    	//{{AFX_MSG(CShowJPG)
    	virtual BOOL OnInitDialog();
    	afx_msg void OnPaint();
    	//}}AFX_MSG
    	DECLARE_MESSAGE_MAP()
    };
    
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_SHOWJPG_H__59AA2FD8_96B7_11D3_B055_00A0C9D7E71C__INCLUDED_)


    Code:
    // ShowJPG.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "Property.h"  // This is my Application class -> replace!
    #include "ShowJPG.h"
    #include <atlconv.h>
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // CShowJPG dialog
    HRESULT _AfxCreateStreamOnFile(LPCTSTR pszPath, LPSTREAM* ppstm,
    	LONG* plSize)
    {
    	ASSERT(pszPath != NULL);
    	ASSERT(ppstm != NULL);
    
    	*ppstm = NULL;
    
    	if (plSize != NULL)
    		*plSize = 0;
    
    	CFileStatus fstatus;
    	CFile file;
    	HRESULT hr = E_FAIL;
    	LONG cb;
    
    	if (file.Open(pszPath, CFile::modeRead) &&
    		file.GetStatus(pszPath, fstatus) &&
    		((cb = fstatus.m_size) != -1))
    	{
    		HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
    		LPVOID pvData = NULL;
    
    		if (hGlobal != NULL)
    		{
    			if ((pvData = GlobalLock(hGlobal)) != NULL)
    			{
    				BOOL bRead = (file.ReadHuge(pvData, cb) == (ULONG)cb);
    				GlobalUnlock(hGlobal);
    
    				if (bRead &&
    					SUCCEEDED(hr = CreateStreamOnHGlobal(hGlobal, TRUE, ppstm)))
    				{
    					ASSERT(*ppstm != NULL);
    					if (plSize != NULL)
    						*plSize = fstatus.m_size;
    					hr = S_OK;
    				}
    			}
    
    			if (FAILED(hr))
    				GlobalFree(hGlobal);
    		}
    		else
    		{
    			hr = E_OUTOFMEMORY;
    		}
    	}
    	else
    	{
    		hr = E_ACCESSDENIED;
    	}
    
    	return hr;
    }
    
    
    CShowJPG::CShowJPG(CWnd* pParent /*=NULL*/)
    	: CDialog(CShowJPG::IDD, pParent)
    {
    	//{{AFX_DATA_INIT(CShowJPG)
    		// NOTE: the ClassWizard will add member initialization here
    	//}}AFX_DATA_INIT
    }
    
    
    void CShowJPG::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(CShowJPG)
    		// NOTE: the ClassWizard will add DDX and DDV calls here
    	//}}AFX_DATA_MAP
    }
    
    
    BEGIN_MESSAGE_MAP(CShowJPG, CDialog)
    	//{{AFX_MSG_MAP(CShowJPG)
    	ON_WM_PAINT()
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CShowJPG message handlers
    
    BOOL CShowJPG::OnInitDialog() 
    {
    	EnableAutomation();
    	CDialog::OnInitDialog();
    	
    	// TODO: Add extra initialization here
    	CString str = "D:\\Temp\\Image.jpg";
    	SetFile(str);
    	CreateBitmap();
    	return TRUE;  // return TRUE unless you set the focus to a control
    	              // EXCEPTION: OCX Property Pages should return FALSE
    }
    
    void CShowJPG::OnPaint() 
    {
    	CPaintDC dc(this); // device context for painting
    	CRect r;
    	GetClientRect(r);
    	m_Picture.Render(&dc,r,r);
    	//dc.BitBlt(0,0,m_BmpRect.Width(), m_BmpRect.Height(),&m_DC,0,0,SRCCOPY);
    }

    A little feature:
    If you want to convert jpg into bmp Use CreateBitmap()
    Then u can BitBlt the Bitmap!

    Code:
    void CShowJPG::CreateBitmap()
    {
    	CDC* pDC = CDC::FromHandle(::GetDC(NULL));
    	if( !m_DC.GetSafeHdc() )
    	{
    		m_DC.CreateCompatibleDC(pDC);
    	}
    	//GetControlSize( (int*)&m_BmpRect.right,(int*)&m_BmpRect.bottom);
    	CSize size;
    	m_Picture.m_pPict->get_Width(&size.cx);
    	m_Picture.m_pPict->get_Height(&size.cy);
    	pDC->HIMETRICtoLP( &size) ;
    	m_Bmp.DeleteObject();
    	m_BmpRect.SetRect(0,0,size.cx,size.cy);
    	m_Bmp.CreateCompatibleBitmap(pDC,m_BmpRect.Width(),m_BmpRect.Height());
    	if( m_Bmp.m_hObject == NULL)
    		return;
    	m_pOld=m_DC.SelectObject(&m_Bmp);
    	m_Picture.Render(&m_DC,m_BmpRect,m_BmpRect);
    	::ReleaseDC(NULL,pDC->GetSafeHdc());
    	Invalidate();
    }
    
    void CShowJPG::SetFile( CString& strPath)
    {
    	LPSTREAM pstm = NULL;
    	LONG lSize;
    	HRESULT hr;
    
    	if (FAILED(hr = _AfxCreateStreamOnFile(strPath, &pstm, &lSize)))
    	{
    		UINT idsText;
    		CString strText;
    		CString strCaption;
    
    		switch (GetScode(hr))
    		{
    		case E_OUTOFMEMORY:
    			idsText = AFX_IDP_PICTURETOOLARGE;
    			break;
    
    		case E_ACCESSDENIED:
    			idsText = AFX_IDP_PICTURECANTOPEN;
    			break;
    
    		default:
    			idsText = AFX_IDP_PICTUREREADFAILED;
    			break;
    		}
    
    		AfxFormatString1(strText, idsText, strPath);
    		strCaption.LoadString(AFX_IDS_PICTURE_PPG_CAPTION);
    		MessageBox(strText, strCaption, MB_OK | MB_ICONEXCLAMATION);
    		SetFocus();
    		return;
    	}
    
    	ASSERT(pstm != NULL);
    
    	//LPPICTURE pPict = ;
    	if (SUCCEEDED(::OleLoadPicture(pstm, lSize, FALSE, IID_IPicture, (LPVOID *)&m_Picture.m_pPict)))
    	{
    			LPPICTUREDISP pPictDisp = NULL;
    			if (SUCCEEDED(m_Picture.m_pPict->QueryInterface(IID_IPictureDisp,
    					(LPVOID*)&pPictDisp)))
    			{
    				ASSERT(pPictDisp != NULL);
    
    				// if (m_pPictDisp != NULL)
    				//	m_pPictDisp->Release();
    
    				 if (m_Picture.GetPictureDispatch() != NULL)
    					m_Picture.GetPictureDispatch()->Release();
    	
    				m_Picture.SetPictureDispatch(pPictDisp);
    		}
    	}
    	else
    	{
    		CString strText;
    		CString strCaption;
    		AfxFormatString1(strText, AFX_IDP_PICTURECANTLOAD, strPath);
    		strCaption.LoadString(AFX_IDS_PICTURE_PPG_CAPTION);
    		MessageBox(strText, strCaption, MB_OK | MB_ICONEXCLAMATION);
    		SetFocus();
    	}
    
    	pstm->Release();
    }

    If you have any problems, i can send you the whole project [email:Gerd.Mayer@Klar-GmbH.de]

    Good luck!

    ( U can also use .gif, .dib, .bmp, .cur, .ico, .emf and .wmf )


    Gerd
    Last edited by 2kaud; January 5th, 2019 at 04:58 AM. Reason: Added code tags

  3. #3
    Join Date
    Jan 2019
    Posts
    3

    Re: Display image.jpg in MFC

    Hello, Gerd Mayer

    Amazing Solution you given here.
    Thanks a Lot!

  4. #4
    Join Date
    Jan 2019
    Posts
    3

    Re: Display image.jpg in MFC

    Is there any solution for PNG format image?
    I want to add PNG image in MFC dialog in c++ platform.
    Please given me proper solution for this problem.

  5. #5
    Join Date
    Jan 2009
    Posts
    271

    Re: Display image.jpg in MFC

    You can load any kind of image file, including png file using CImage to load image file into CBitmap object:
    Code:
    CImage image;
    CBitmap Bitmap;
    CString sFileName = _T("C:\\Test\\Image.png");
    if(S_OK == image.Load(sFileName))
    {
        Bitmap.Attach(image.Detach());
    }
    and rendering CBitmap is another matter ...

  6. #6
    Join Date
    Jan 2019
    Posts
    3

    Re: Display image.jpg in MFC


    Thanks a lot to mesajflaviu for replaying.
    I got solution for all format.
    For that i need to change Picture Control Type from frame to Bitmap.
    and it's fine to work through CImage.
    I do like this:

    Code:
            CStatic* pWnd = (CStatic*)GetDlgItem(IDC_STATIC);
    	CImage image;
    	image.Load(_T("png.png"));
    	int O_Height = image.GetHeight();
    	int O_Width = image.GetWidth();
    	HBITMAP hBmp = image.Detach();
    	pWnd->SetBitmap(hBmp);
    	pWnd->SetWindowPos(NULL, 5, 10, O_Width, O_Height, SWP_NOACTIVATE | SWP_NOZORDER);
    => Load any images at runtime.
    It's short solution for all images or whether you can going through Sir Gerd Mayer logic.
    Last edited by 2kaud; January 7th, 2019 at 08:03 AM. Reason: Added code tags

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,274

    Re: Display image.jpg in MFC

    Quote Originally Posted by Jaix View Post
    ...
    I do like this:

    Code:
            CStatic* pWnd = (CStatic*)GetDlgItem(IDC_STATIC);
    	CImage image;
    	image.Load(_T("png.png"));
    	int O_Height = image.GetHeight();
    	int O_Width = image.GetWidth();
    	HBITMAP hBmp = image.Detach();
    	pWnd->SetBitmap(hBmp);
    	pWnd->SetWindowPos(NULL, 5, 10, O_Width, O_Height, SWP_NOACTIVATE | SWP_NOZORDER);
    => Load any images at runtime.
    It's short solution for all images or whether you can going through Sir Gerd Mayer logic.
    It is good that have solved your problem!
    It could be better if you used CODE tags around the code snippets you post to the Forum!

    And, BTW, why do you avoid to create and use the control member variables (it is very easy to create them with the MFC Class Wizard!)?
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)