XDisplayImage
A frequent requirement in an application is to load and display images – typically, bmp, gif, jpg, png, and ico formats. The images may be embedded in the application as a resource, or may be external, loaded from a file on disk, or even loaded from a remote file via the internet. XDisplayImage implements a mechanism for loading and displaying images in any of these formats, from an embedded resource or external file or url, without requiring any third-party library or DLL.
There are three restrictions:
- On Vista, all icon formats except Vista packed icons can be used with XDisplayImage.
- On Win2000, icon images cannot be used (they can on XP and Vista).
- TIFF files cannot be used.
Demo Application
The demo application shows how XDisplayImage can load and display images from resources (the top four images and the icons are all embedded resources) or from an external file (over the internet):
You may request a specific icon size - if the ico file contains multiple formats - or request the default size (usually 32 x 32). In the demo, the second icon is displayed using the default size.
The next screenshot shows a sample About box, with text of Win32 Static text boxes overlaying the image (which fills the entire client area):
XDisplayImage APIs
// DisplayImageFromResource() // // Purpose: Load the image from the resource specified by nImageResourceId // and display it in the area specified by rect. // // Parameters: hWndParent - parent window // hResModule - handle of module that contains resource // nImageResourceType - resource type (can be any integer value not // already used by system) // nImageResourceId - resource id // nCtrlId - child control identifier // rect - position of image window in client // coordinates // bTransparent - TRUE = set image background transparent // bSendToBack - TRUE = move image behind other controls // // Returns: HWND - handle of window created to display image, or 0 if error; // the caller is responsible for destroying this window when // it is no longer needed.Or this API for string resource IDs:
// DisplayImageFromResource() // // Purpose: Load the image from the resource specified by lpszImageResourceId // and display it in the area specified by rect. // // Parameters: hWndParent - parent window // hResModule - handle of module that contains resource // lpszImageResourceType - resource type - can be any string or // MAKEINTRESOURCE value // lpszImageResourceId - resource id - can be any string or // MAKEINTRESOURCE value // nCtrlId - child control identifier // rect - position of image window in client // coordinates // bTransparent - TRUE = set image background transparent // bSendToBack - TRUE = move image behind other controls // // Returns: HWND - handle of window created to display image, or 0 if error; // the caller is responsible for destroying this window when // it is no longer needed.To display an image from a file or URL, use this API:
// DisplayImageFromUrl() // // Purpose: Load the image from the url specified by lpszImageUrl // and display it in the area specified by rect. // // Parameters: hWndParent - parent window // lpszImageUrl - url string // nCtrlId - child control identifier // rect - position of image window in client // coordinates // bTransparent - TRUE = set image background transparent // bSendToBack - TRUE = move image behind other controls // // Returns: HWND - handle of window created to display image, or 0 if error; // the caller is responsible for destroying this window when // it is no longer needed.
In addition to the http:// protocol, the file:// protocol
may also be used:
file:///C:/temp/ie8_logo.gifTo display an icon from a resource, use this API for integer resource IDs:
// DisplayIconFromResource() // // Purpose: Load the icon from the resource specified by nImageResourceId // and display it in the area specified by rect. // // Parameters: hWndParent - parent window // hResModule - handle of module that contains resource // nImageResourceId - resource id // nCtrlId - child control identifier // rect - position of image window in client // coordinates // nIconWidth - icon width, or 0 to use system default // nIconHeight - icon height, or 0 to use system default // bTransparent - TRUE = set image background transparent // bSendToBack - TRUE = move image behind other controls // // Returns: HWND - handle of window created to display image, or 0 if error; // the caller is responsible for destroying this window when // it is no longer needed.Or this API for string resource IDs:
// DisplayIconFromResource() // // Purpose: Load the icon from the resource specified by lpszImageResourceId // and display it in the area specified by rect. // // Parameters: hWndParent - parent window // hResModule - handle of module that contains resource // lpszImageResourceId - resource id - can be any string or // MAKEINTRESOURCE value // nCtrlId - child control identifier // rect - position of image window in client // coordinates // nIconWidth - icon width, or 0 to use system default // nIconHeight - icon height, or 0 to use system default // bTransparent - TRUE = set image background transparent // bSendToBack - TRUE = move image behind other controls // // Returns: HWND - handle of window created to display image, or 0 if error; // the caller is responsible for destroying this window when // it is no longer needed.
Tips & Tricks
- XDisplayImage can display all icon types except Vista packed icons.
- You must call
OleInitialize()orCoInitialize()before calling any XDisplayImage API. - Don't forget to destroy the XDisplayImage child window when it's no longer needed.
- You can compile XDisplayImage.cpp with or without
MFC. To compile withMFC, un-comment the line#include "stdafx.h"
at the beginning of XDisplayImage.cpp, and make sure that XDisplayImage.cpp is set to Use precompiled headers - be sure to do this for all configurations.
Revision History
Version 1.3 - 2009 June 11
- You can now overlay control with standard Win32 static text boxes
Version 1.2 - 2009 June 4
- Control now sends WM_LBUTTONDOWN, WM_LBUTTONUP, and WM_MOUSEMOVE messages to parent
- Added VS2008 project
Version 1.1 - 2009 May 29
- Added API for string / MAKEINTRESOURCE
- Added ability to display icons
Version 1.0 - 2009 May 1
- Initial release
Buy latest version
| Buy XDisplayImage 1.3 license – $20.00 per developer |