Solved

MFC: How to display an image in a view

Posted on 2010-11-23
7
481 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 135
Socket Programming (Unix) 8 153
Header of docx file 17 131
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 252
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

733 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