Click here to Skip to main content
Licence CPOL
First Posted 5 Apr 2008
Views 97,560
Downloads 6,571
Bookmarked 49 times

An MFC picture control to dynamically show pictures in a dialog

By | 24 Apr 2008 | Article
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)

About the Author

TEiseler

Tester / Quality Assurance

Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to clear the png image? Pinmemberchaiein18:22 3 May '12  
QuestionLoading from resource Pinmemberthes3cr3t111:00 1 Mar '12  
QuestionMFC Drawing Control Pinmemberp2002ad13:32 7 Dec '11  
Questioni have killed it Pinmemberbawe00035:40 3 Nov '11  
AnswerRe: i have killed it Pinmemberbawe00030:39 4 Nov '11  
QuestionGreat Code but would like Load From Resource PinmemberMember 829929914:36 9 Oct '11  
QuestionSample code for VC++ 6.0 PinmemberJasonAw19:48 6 Oct '11  
Generalyour approval PinmemberMember 78417375:03 14 Apr '11  
Questionimages go out...??? Pinmemberpaspartu_02:01 4 Mar '11  
QuestionHow to use in VC++ 6.0 PinmemberBL Tay16:44 24 Dec '10  
AnswerRe: How to use in VC++ 6.0 PinmemberJPhelps1:54 12 Apr '11  
GeneralMy vote of 5 PinmemberJohn Jasper4:49 25 Oct '10  
GeneralLoad from Resource Pinmemberfritzpas0:44 1 Nov '09  
GeneralIs it possible to get fullscreen on doubleclick PinmemberAssariah kingsly5:30 8 Sep '09  
GeneralProblem with SDI programm (VC++) [modified] Pinmemberkudlaty790:17 27 Jul '09  
Generalfix for CFormview vst2005 Pinmembermhorowit12:05 6 Mar '09  
GeneralRe: fix for CFormview vst2005 PinmemberJPhelps11:05 7 Apr '11  
Generalvc6 owner draw Pinmemberreiphil9:11 25 Feb '09  
QuestionFreeImage PinmemberDougVC12:36 25 Jan '09  
AnswerRe: FreeImage Pinmembernileshup5:15 6 Oct '09  
AnswerRe: FreeImage PinmemberMaster^Tristar20:44 23 Jun '10  
GeneralAwesome, but ratio PinmemberTenguryu5:08 25 Nov '08  
AnswerRe: Awesome, but ratio PinmemberMaster^Tristar22:45 23 Jun '10  
Questioncan not showing image same as canabal [modified] PinmemberVaishali91791:22 16 Oct '08  
AnswerRe: can not showing image same as canabal PinmemberTEiseler10:01 16 Oct '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 24 Apr 2008
Article Copyright 2008 by TEiseler
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid