Click here to Skip to main content
13,199,680 members (67,779 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 4 Aug 2004

Picture Box for WTL

, 4 Aug 2004
Rate this:
Please Sign up or sign in to vote.
An article on how to use the CPictureBox control to load and display *.bmp, *.jpg, *.png and *.gif files.


This code is extremely useful if you want to display images. It loads *.bmp *.jpg *.png and *.gif files from disk or from resources included in your program. The control has scrollbar support and a build in context menu for easy usage which can be disabled.

Using the code

The following example assumes that you want the picture box to be in the client area of the main view.

//  CMainFrm.h
#include <span class="code-string">"PictureBox.h"</span>

class CMainFrame:
  // Other code here
  WTL::CPictureBox  m_PictureBox;

// CMainFrm.cpp
LRESULT CMainFrame::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, 
    LPARAM /*lParam*/, BOOL& /*bHandled*/)
  // Some code
  // Create the Picture Box
  m_hwndClient = m_PictureBox.Create(m_hWnd, rcDefault, NULL,
  // The control is created, now we can load a picture in it.
  // To load an image in the Picture Box use any of the functions below
  // Some more code
The create function of the CPictureBox works just like the create function of any CWindowImpl derived class. By default the control will position the bitmap in the center of the view, and it will use the build in context menu.

The CPictureBox functions

// Load an image from a file
// pszFileName is the path to the image
// the function returns true if succesfull, otherwise it returns false.
bool  LoadBitmapFromFile(LPCTSTR pszFileName);

// Load an image from a resource
// hInstance is the handle to the HINSTANCE you want to load from
// uiIDResource is the ID of the resource
// the function returns true if succesfull, otherwise it returns false.
bool  LoadBitmapFromID(HINSTANCE hInstance, UINT uiIDResource)

// Set the bitmap directly
// hBitmap is the Handle to the bitmap
// if  bOwner is true, the CPictureBox will 
// destroy the hBitamp when it is destroyed
// otherwise you have to clean up the hBitmap yourself
void  SetBitmap(HBITMAP hBitmap, bool bOwner = true)

// Detaches the bitmap from the CPictureBox and returns the bitmap
HBITMAP  GetBitmap()

// returns a save handle to the bitmap
HBITMAP  GetSaveBitmap()const

// if bCenter is true, the control will position 
// the bitmap in the center of the view
// otherwise the bitmap will be positioned in the 
// top left corner of the view
void  CenterPicture(bool bCenter)

// if bStretch is true, the control will stretct the 
// bitmap so that it fits the view
// otherwise the bitmap will be drawn normaly
void  StretchPicture(bool bStretch)

// if bUseMenu is true, the control will use the build in context menu
// otherwise the context menu will not be used
void  UseMenu(bool bUseMenu)

Points of Interest

I wrote this code because WTL did not have any classes that could display *.jpg, *.png, *.gif or had scrollbar support.

Additional Information

For additional information, questions and bug report, visit my website:, or contact me at


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Technical Lead Premotion, Liones Internet BV
Netherlands Netherlands
I am Bert Willems a 24 year old developer living in Drachten, The Netherlands. I was born on 4 July 1985 in Drachten. My great passion is development of software, I simply love doing it. Since I was young I have been fascinated by technique and constructing things, first with Lego and later on things for the computer. I started with simple websites and some scripting of games, and then I decided I would make a career out of it and started with the development of business software. I am not sure why I love programming so much, but I know it has something to do with expressing my creativity in it.

You may also be interested in...

Comments and Discussions

GeneralNew location for the article downloads Pin
Trilobyte30-Apr-09 0:18
memberTrilobyte30-Apr-09 0:18 
QuestionHow can i use CScrollWindowImpl in WTL 8.0 Pin
kamrul04527-Feb-08 9:38
memberkamrul04527-Feb-08 9:38 
AnswerRe: How can i use CScrollWindowImpl in WTL 8.0 Pin
Paul Selormey1-Mar-09 23:07
memberPaul Selormey1-Mar-09 23:07 
GeneralDoom 3 rocks Pin
technomanceraus4-Aug-04 18:45
membertechnomanceraus4-Aug-04 18:45 
GeneralRe: Doom 3 rocks Pin
Mr. TriloByte5-Aug-04 8:58
memberMr. TriloByte5-Aug-04 8:58 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 5 Aug 2004
Article Copyright 2004 by Trilobyte
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid