Solved

MFC: How to display an image in a view

Posted on 2010-11-23
7
475 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
computer science syllabus 3 88
Unable to start eclipse ? 17 153
How to convert Structure to vector of byte and vice versa 5 24
Outlook 13 41
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 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