Solved

MFC: How to display an image in a view

Posted on 2010-11-23
7
457 Views
Last Modified: 2012-06-22
Dear experts,

I have an MFC program that displays lines of text in a view. But I would need to display pictures besides the text. Is there any simple way to do that?

Thanks.
0
Comment
Question by:tantormedia
7 Comments
 
LVL 4

Expert Comment

by:incerc
ID: 34196958
0
 
LVL 5

Expert Comment

by:Xper4net
ID: 34197004
It can be done in virtual OnDraw. But then, difficulty level depends on image type. If this is a bmp, no concern. If you want to display a jpg or a gif, there's no standard function, you must use an external library.
0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 250 total points
ID: 34198151
If you need to read a JPEG, GIF, orBMP, you can use the MFC class CImage.  
However, CImage uses GDI+.  That means the resulting application won't run on a Windows operating systems that pre-dates XP without downloading some sort of an update.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:tantormedia
ID: 34198404
How about PNG?
0
 
LVL 5

Assisted Solution

by:Xper4net
Xper4net earned 250 total points
ID: 34198489
@HooKooDooKu : you're right...I've forgotten CImage improvements.

Then, solution should be:
class CMyView : public CView
{
protected: // create from serialization only
	CMyView();
	DECLARE_DYNCREATE(CMyView)

// Overrides
public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
// Implementation
public:
	virtual ~CMyView();
// Generated message map functions
protected:
	DECLARE_MESSAGE_MAP()

 CImage m_Image ;
};

CMyView::CMyView()
{
    m_Image.Load("myImage.jpg") ;
}

void CMyView::OnDraw(CDC* /*pDC*/)
{
   m_Image.Draw(pDC->GetSafeHdc(), 0, 0) ; // consider overrides if you want to stretch image
}

Open in new window

0
 
LVL 5

Expert Comment

by:Xper4net
ID: 34198496
CImage handles PNG
0
 

Author Closing Comment

by:tantormedia
ID: 34199064
Thank you very much.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now