Solved

MFC: How to display an image in a view

Posted on 2010-11-23
7
464 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

895 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

15 Experts available now in Live!

Get 1:1 Help Now