Click here to Skip to main content
15,885,141 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 325.6K   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

 
QuestionSample code for VC++ 6.0 Pin
JasonAw6-Oct-11 19:48
JasonAw6-Oct-11 19:48 
Generalyour approval Pin
Member 784173714-Apr-11 5:03
Member 784173714-Apr-11 5:03 
Questionimages go out...??? Pin
paspartu_04-Mar-11 2:01
paspartu_04-Mar-11 2:01 
QuestionHow to use in VC++ 6.0 Pin
BL Tay24-Dec-10 16:44
BL Tay24-Dec-10 16:44 
AnswerRe: How to use in VC++ 6.0 Pin
JPhelps12-Apr-11 1:54
JPhelps12-Apr-11 1:54 
GeneralMy vote of 5 Pin
John Jasper25-Oct-10 4:49
John Jasper25-Oct-10 4:49 
GeneralLoad from Resource Pin
fritzpas1-Nov-09 0:44
fritzpas1-Nov-09 0:44 
GeneralIs it possible to get fullscreen on doubleclick Pin
Assariah kingsly8-Sep-09 5:30
Assariah kingsly8-Sep-09 5:30 
GeneralProblem with SDI programm (VC++) [modified] Pin
kudlaty7927-Jul-09 0:17
kudlaty7927-Jul-09 0:17 
Generalfix for CFormview vst2005 Pin
mhorowit6-Mar-09 12:05
mhorowit6-Mar-09 12:05 
GeneralRe: fix for CFormview vst2005 Pin
JPhelps7-Apr-11 11:05
JPhelps7-Apr-11 11:05 
Generalvc6 owner draw Pin
reiphil25-Feb-09 9:11
reiphil25-Feb-09 9:11 
QuestionFreeImage Pin
DougVC25-Jan-09 12:36
DougVC25-Jan-09 12:36 
AnswerRe: FreeImage Pin
nileshup6-Oct-09 5:15
nileshup6-Oct-09 5:15 
AnswerRe: FreeImage Pin
Master^Tristar23-Jun-10 20:44
Master^Tristar23-Jun-10 20:44 
GeneralAwesome, but ratio Pin
Tenguryu25-Nov-08 5:08
Tenguryu25-Nov-08 5:08 
AnswerRe: Awesome, but ratio Pin
Master^Tristar23-Jun-10 22:45
Master^Tristar23-Jun-10 22:45 
GeneralRe: Awesome, but ratio Pin
showtime27-Feb-19 11:23
showtime27-Feb-19 11:23 
Questioncan not showing image same as canabal [modified] Pin
Vaishali917916-Oct-08 1:22
Vaishali917916-Oct-08 1:22 
AnswerRe: can not showing image same as canabal Pin
TEiseler16-Oct-08 10:01
TEiseler16-Oct-08 10:01 
GeneralImage is not displayed Pin
Atox5-Sep-08 5:47
Atox5-Sep-08 5:47 
QuestionIs there a memory leak in PictureCtrl.cpp ? Pin
bertszoghy26-Jun-08 3:53
bertszoghy26-Jun-08 3:53 
AnswerRe: Is there a memory leak in PictureCtrl.cpp ? Pin
TEiseler26-Jun-08 6:49
TEiseler26-Jun-08 6:49 
Generalunresolved external symbol Pin
Diego200218-Jun-08 8:00
Diego200218-Jun-08 8:00 
GeneralRe: unresolved external symbol Pin
TEiseler18-Jun-08 11:46
TEiseler18-Jun-08 11:46 

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.