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
Your image will be automatically sized to the size of your control, regardless of the aspect ratio.
class CPictureCtrl :
//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
virtual void PreSubclassWindow();
//Draws the Control
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual BOOL OnEraseBkgnd(CDC* pDC);
//Internal image stream buffer
//Control flag if a pic is loaded
//GDI Plus Token
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.