Click here to Skip to main content
15,307,716 members
Articles / Desktop Programming / MFC
Posted 5 Apr 2008


86 bookmarked

An MFC picture control to dynamically show pictures in a dialog

Rate me:
Please Sign up or sign in to vote.
4.65/5 (43 votes)
24 Apr 2008CPOL1 min read
An MFC picture control to dynamically show pictures in a dialog.


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.


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




    //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();

    virtual void PreSubclassWindow();

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


    //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.


  • 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.


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


About the Author

Tester / Quality Assurance
Germany Germany
No Biography provided

Comments and Discussions

QuestionLoading from resource Pin
Michael Haephrati16-May-20 7:51
mvaMichael Haephrati16-May-20 7:51 
QuestionRead Image Data from Database Pin
Wotnix8-Jun-19 3:59
MemberWotnix8-Jun-19 3:59 
AnswerRe: Read Image Data from Database Pin
ninpo8-Apr-22 6:28
Memberninpo8-Apr-22 6:28 
QuestionKeeping image aspect ratio inside control Pin
Jacques Fourie30-Mar-15 23:47
MemberJacques Fourie30-Mar-15 23:47 
AnswerRe: Keeping image aspect ratio inside control Pin
showtime27-Feb-19 11:06
Membershowtime27-Feb-19 11:06 
SuggestionIncluding GdiPlus.lib Pin
Giovefi10-Dec-14 4:18
MemberGiovefi10-Dec-14 4:18 
QuestionDisplay of formats other than jpg Pin
jdbuckley12-Jun-14 8:56
Memberjdbuckley12-Jun-14 8:56 
SuggestionLoading from Resource Pin
itskumaranand4-Jun-14 21:44
Memberitskumaranand4-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
Memberandrei91127-Jan-14 8:27 
QuestionMemory leak is coming! Pin
Sun-Mi Kang29-Oct-13 19:27
MemberSun-Mi Kang29-Oct-13 19:27 
AnswerRe: Memory leak is coming! Pin
Member 471758926-Aug-14 1:28
MemberMember 471758926-Aug-14 1:28 
GeneralRe: Memory leak is coming! Pin
prodelsoft23-Nov-16 2:35
Memberprodelsoft23-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
Memberpassionkk8-Sep-13 22:13 
QuestionCan not download Pin
potter_tang17-Jul-13 13:38
Memberpotter_tang17-Jul-13 13:38 
SuggestionRegarding resource image loading. Pin
slack721920-Feb-13 2:13
Memberslack721920-Feb-13 2:13 
SuggestionResource Image Loading Pin
KimJinWoo7-Oct-12 0:51
MemberKimJinWoo7-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
Memberenzo.faffa28-May-12 3:43 
QuestionHow to clear the png image? Pin
chaiein3-May-12 18:22
Memberchaiein3-May-12 18:22 
QuestionLoading from resource Pin
thes3cr3t11-Mar-12 11:00
Memberthes3cr3t11-Mar-12 11:00 
AnswerRe: Loading from resource Pin
Simon Langdon28-Jun-16 12:52
MemberSimon Langdon28-Jun-16 12:52 
QuestionMFC Drawing Control Pin
p2002ad7-Dec-11 13:32
Memberp2002ad7-Dec-11 13:32 
Questioni have killed it Pin
bawe00033-Nov-11 5:40
Memberbawe00033-Nov-11 5:40 
AnswerRe: i have killed it Pin
bawe00034-Nov-11 0:39
Memberbawe00034-Nov-11 0:39 
QuestionGreat Code but would like Load From Resource Pin
Member 82992999-Oct-11 14:36
MemberMember 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.