Click here to Skip to main content
12,248,681 members (46,147 online)
Click here to Skip to main content
Add your own
alternative version


75 bookmarked

An MFC picture control to dynamically show pictures in a dialog

, 24 Apr 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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

You may also be interested in...

Comments and Discussions

QuestionKeeping image aspect ratio inside control Pin
Jacques Fourie31-Mar-15 0:47
memberJacques Fourie31-Mar-15 0:47 
SuggestionIncluding GdiPlus.lib Pin
Giovefi10-Dec-14 5:18
memberGiovefi10-Dec-14 5:18 
QuestionDisplay of formats other than jpg Pin
jdbuckley12-Jun-14 9:56
memberjdbuckley12-Jun-14 9:56 
SuggestionLoading from Resource Pin
itskumaranand4-Jun-14 22:44
memberitskumaranand4-Jun-14 22:44 
QuestionMFC Picture control to display pictures in windows ce using vs2008 (mfc smart device application Pin
andrei91127-Jan-14 9:27
memberandrei91127-Jan-14 9:27 
QuestionMemory leak is coming! Pin
sun mi Kang29-Oct-13 20:27
membersun mi Kang29-Oct-13 20:27 
AnswerRe: Memory leak is coming! Pin
Member 471758926-Aug-14 2:28
memberMember 471758926-Aug-14 2:28 
GeneralMy vote of 5 Pin
Member 1030799913-Oct-13 16:23
memberMember 1030799913-Oct-13 16:23 
QuestionOnEraseBkgnd Pin
passionkk8-Sep-13 23:13
memberpassionkk8-Sep-13 23:13 
QuestionCan not download Pin
potter_tang17-Jul-13 14:38
memberpotter_tang17-Jul-13 14:38 
SuggestionRegarding resource image loading. Pin
slack721920-Feb-13 3:13
memberslack721920-Feb-13 3:13 
SuggestionResource Image Loading Pin
KimJinWoo7-Oct-12 1:51
memberKimJinWoo7-Oct-12 1:51 
QuestionNice but I need loading from resource and a way to set image as background Pin
enzo.faffa28-May-12 4:43
memberenzo.faffa28-May-12 4:43 
QuestionHow to clear the png image? Pin
chaiein3-May-12 19:22
memberchaiein3-May-12 19:22 
QuestionLoading from resource Pin
thes3cr3t11-Mar-12 12:00
memberthes3cr3t11-Mar-12 12:00 
QuestionMFC Drawing Control Pin
p2002ad7-Dec-11 14:32
memberp2002ad7-Dec-11 14:32 
Questioni have killed it Pin
bawe00033-Nov-11 6:40
memberbawe00033-Nov-11 6:40 
AnswerRe: i have killed it Pin
bawe00034-Nov-11 1:39
memberbawe00034-Nov-11 1:39 
QuestionGreat Code but would like Load From Resource Pin
Member 82992999-Oct-11 15:36
memberMember 82992999-Oct-11 15:36 
QuestionSample code for VC++ 6.0 Pin
JasonAw6-Oct-11 20:48
memberJasonAw6-Oct-11 20:48 
Generalyour approval Pin
Member 784173714-Apr-11 6:03
memberMember 784173714-Apr-11 6:03 
Questionimages go out...??? Pin
paspartu_04-Mar-11 3:01
memberpaspartu_04-Mar-11 3:01 
QuestionHow to use in VC++ 6.0 Pin
BL Tay24-Dec-10 17:44
memberBL Tay24-Dec-10 17:44 
AnswerRe: How to use in VC++ 6.0 Pin
JPhelps12-Apr-11 2:54
memberJPhelps12-Apr-11 2:54 
GeneralMy vote of 5 Pin
John Jasper25-Oct-10 5:49
memberJohn Jasper25-Oct-10 5:49 
GeneralLoad from Resource Pin
fritzpas1-Nov-09 1:44
memberfritzpas1-Nov-09 1:44 
GeneralIs it possible to get fullscreen on doubleclick Pin
Assariah kingsly8-Sep-09 6:30
memberAssariah kingsly8-Sep-09 6:30 
GeneralProblem with SDI programm (VC++) [modified] Pin
kudlaty7927-Jul-09 1:17
memberkudlaty7927-Jul-09 1:17 
Generalfix for CFormview vst2005 Pin
mhorowit6-Mar-09 13:05
membermhorowit6-Mar-09 13:05 
GeneralRe: fix for CFormview vst2005 Pin
JPhelps7-Apr-11 12:05
memberJPhelps7-Apr-11 12:05 
Generalvc6 owner draw Pin
reiphil25-Feb-09 10:11
memberreiphil25-Feb-09 10:11 
QuestionFreeImage Pin
DougVC25-Jan-09 13:36
memberDougVC25-Jan-09 13:36 
AnswerRe: FreeImage Pin
nileshup6-Oct-09 6:15
membernileshup6-Oct-09 6:15 
AnswerRe: FreeImage Pin
Master^Tristar23-Jun-10 21:44
memberMaster^Tristar23-Jun-10 21:44 
GeneralAwesome, but ratio Pin
Tenguryu25-Nov-08 6:08
memberTenguryu25-Nov-08 6:08 
AnswerRe: Awesome, but ratio Pin
Master^Tristar23-Jun-10 23:45
memberMaster^Tristar23-Jun-10 23:45 
Questioncan not showing image same as canabal [modified] Pin
Vaishali917916-Oct-08 2:22
memberVaishali917916-Oct-08 2:22 
AnswerRe: can not showing image same as canabal Pin
TEiseler16-Oct-08 11:01
memberTEiseler16-Oct-08 11:01 
GeneralImage is not displayed Pin
Atox5-Sep-08 6:47
memberAtox5-Sep-08 6:47 
QuestionIs there a memory leak in PictureCtrl.cpp ? Pin
bertszoghy26-Jun-08 4:53
memberbertszoghy26-Jun-08 4:53 
AnswerRe: Is there a memory leak in PictureCtrl.cpp ? Pin
TEiseler26-Jun-08 7:49
memberTEiseler26-Jun-08 7:49 
Generalunresolved external symbol Pin
Diego200218-Jun-08 9:00
memberDiego200218-Jun-08 9:00 
GeneralRe: unresolved external symbol Pin
TEiseler18-Jun-08 12:46
memberTEiseler18-Jun-08 12:46 
GeneralRe: unresolved external symbol Pin
Diego200218-Jun-08 21:40
memberDiego200218-Jun-08 21:40 
GeneralRe: unresolved external symbol Pin
TEiseler19-Jun-08 8:33
memberTEiseler19-Jun-08 8:33 
QuestionHow to use the control in VC6.0 Pin
jianan11-Jun-08 5:56
memberjianan11-Jun-08 5:56 
AnswerRe: How to use the control in VC6.0 Pin
TEiseler11-Jun-08 8:14
memberTEiseler11-Jun-08 8:14 
QuestionCan we load images from server Pin
dprem4-Jun-08 17:59
memberdprem4-Jun-08 17:59 
AnswerRe: Can we load images from server Pin
TEiseler5-Jun-08 9:58
memberTEiseler5-Jun-08 9:58 
QuestionLoading picture in ActiveX using static control Pin
jagannathan thiruvengadathan27-May-08 0:25
memberjagannathan thiruvengadathan27-May-08 0:25 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 24 Apr 2008
Article Copyright 2008 by TEiseler
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid