Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC
Article

An MFC picture control to dynamically show pictures in a dialog

Rate me:
Please Sign up or sign in to vote.
4.67/5 (44 votes)
24 Apr 2008CPOL1 min read 324.1K   31.4K   88   68
An MFC picture control to dynamically show pictures in a dialog.

Introduction

This article describes an MFC control that makes it possible to display any picture in a standard image format (like BMP, GIF, JPEG, etc...) on a dialog.

Background

It took me some time to search for a picture control for MFC, but unfortunately, I found none that actually worked for me. So, I decided to make myself a flexible and lightweight picture control to display all types of images.

Using the code

This control internally uses the GDI+ library. So, please make sure to include GdiPlus.lib to your include libraries.

To use this control, create a static text control with the dialog designer of Visual C++. After that, assign a control member variable of type CPictureCtrl to it.

Now, you can load a picture on your control. Do that by calling one of the various CPictureCtrl::LoadFrom... functions. Use the one that suits your needs. The control should automatically update to the new image.

To clear the image, call CPictureCtrl::FreeImage.

Your image will be automatically sized to the size of your control, regardless of the aspect ratio.

class CPictureCtrl :
    public CStatic
{
public:

    //Constructor
    CPictureCtrl(void);

    //Destructor
    ~CPictureCtrl(void);

public:

    //Loads an image from a file
    BOOL LoadFromFile(CString &szFilePath);

    //Loads an image from an IStream interface
    BOOL LoadFromStream(IStream* piStream);

    //Loads an image from a byte stream;
    BOOL LoadFromStream(BYTE* pData, size_t nSize);

    //Loads an image from a Resource
//     BOOL LoadFromResource(HMODULE hModule, LPCTSTR lpName, LPCTSTR lpType);

    //Overload - Single load function
    BOOL Load(CString &szFilePath);
    BOOL Load(IStream* piStream);
    BOOL Load(BYTE* pData, size_t nSize);
//     BOOL Load(HMODULE hModule, LPCTSTR lpName, LPCTSTR lpType);

    //Frees the image data
    void FreeData();

protected:
    virtual void PreSubclassWindow();

    //Draws the Control
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    virtual BOOL OnEraseBkgnd(CDC* pDC);

private:

    //Internal image stream buffer
    IStream* m_pStream;

    //Control flag if a pic is loaded
    BOOL m_bIsPicLoaded;

    //GDI Plus Token
    ULONG_PTR m_gdiplusToken;
};

Points of interest

The control is based on subclassing a CStatic control. Therefore, you will have all the functionality of this control, but it will not display any text. The usage of the GDI+ library makes it possible to work with many modern types of image files.

History

  • 1.0 - Initial release.
  • 1.1 - A bug when drawing the control without a loaded image was corrected.
  • 1.2 - A bug when drawing the control was corrected.
  • Loading an image from a resource is disabled due to problems recognizing it correctly as an image.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Tester / Quality Assurance
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLoading from resource Pin
Michael Haephrati16-May-20 7:51
professionalMichael Haephrati16-May-20 7:51 
QuestionRead Image Data from Database Pin
Wotnix8-Jun-19 3:59
Wotnix8-Jun-19 3:59 
AnswerRe: Read Image Data from Database Pin
ninpo8-Apr-22 6:28
ninpo8-Apr-22 6:28 
QuestionKeeping image aspect ratio inside control Pin
Jacques Fourie30-Mar-15 23:47
Jacques Fourie30-Mar-15 23:47 
AnswerRe: Keeping image aspect ratio inside control Pin
showtime27-Feb-19 11:06
showtime27-Feb-19 11:06 
SuggestionIncluding GdiPlus.lib Pin
Giovefi10-Dec-14 4:18
Giovefi10-Dec-14 4:18 
QuestionDisplay of formats other than jpg Pin
jdbuckley12-Jun-14 8:56
jdbuckley12-Jun-14 8:56 
SuggestionLoading from Resource Pin
itskumaranand4-Jun-14 21:44
itskumaranand4-Jun-14 21:44 
QuestionMFC Picture control to display pictures in windows ce using vs2008 (mfc smart device application Pin
andrei91127-Jan-14 8:27
andrei91127-Jan-14 8:27 
QuestionMemory leak is coming! Pin
Sun-Mi Kang29-Oct-13 19:27
Sun-Mi Kang29-Oct-13 19:27 
AnswerRe: Memory leak is coming! Pin
Member 471758926-Aug-14 1:28
Member 471758926-Aug-14 1:28 
GeneralRe: Memory leak is coming! Pin
prodelsoft23-Nov-16 2:35
prodelsoft23-Nov-16 2:35 
GeneralMy vote of 5 Pin
Member 1030799913-Oct-13 15:23
professionalMember 1030799913-Oct-13 15:23 
QuestionOnEraseBkgnd Pin
passionkk8-Sep-13 22:13
passionkk8-Sep-13 22:13 
QuestionCan not download Pin
potter_tang17-Jul-13 13:38
potter_tang17-Jul-13 13:38 
SuggestionRegarding resource image loading. Pin
slack721920-Feb-13 2:13
slack721920-Feb-13 2:13 
SuggestionResource Image Loading Pin
KimJinWoo7-Oct-12 0:51
KimJinWoo7-Oct-12 0:51 
QuestionNice but I need loading from resource and a way to set image as background Pin
enzo.faffa28-May-12 3:43
enzo.faffa28-May-12 3:43 
QuestionHow to clear the png image? Pin
chaiein3-May-12 18:22
chaiein3-May-12 18:22 
QuestionLoading from resource Pin
thes3cr3t11-Mar-12 11:00
thes3cr3t11-Mar-12 11:00 
AnswerRe: Loading from resource Pin
Simon Langdon28-Jun-16 12:52
Simon Langdon28-Jun-16 12:52 
QuestionMFC Drawing Control Pin
p2002ad7-Dec-11 13:32
p2002ad7-Dec-11 13:32 
Questioni have killed it Pin
bawe00033-Nov-11 5:40
bawe00033-Nov-11 5:40 
AnswerRe: i have killed it Pin
bawe00034-Nov-11 0:39
bawe00034-Nov-11 0:39 
Ok i have the solution.
The pictures size was to big. 32MB is not suitable to show in an dialog.
QuestionGreat Code but would like Load From Resource Pin
Member 82992999-Oct-11 14:36
Member 82992999-Oct-11 14:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.