Click here to Skip to main content
13,556,988 members
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 15 Jun 2007

Easy way to Load Image in MFC dialog

, 15 Jun 2007
Rate this:
Please Sign up or sign in to vote.
This artcle describes how to load image into dialogs easily


This article tells how to link a bitmap image into dialog background.

How to do...?

Create a dialog based application, open the dialog.cpp file and go to OnPaint function, it looks like below coding...

void CLoad_ImageDlg::OnPaint() 
 if (IsIconic())
  CPaintDC dc(this); // device context for painting
  SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  // Center icon in client rectangle
  int cxIcon = GetSystemMetrics(SM_CXICON);
  int cyIcon = GetSystemMetrics(SM_CYICON);
  CRect rect;
  int x = (rect.Width() - cxIcon + 1) / 2;
  int y = (rect.Height() - cyIcon + 1) / 2;
  // Draw the icon
  dc.DrawIcon(x, y, m_hIcon);

just erase the coding (if you dont need of icon for your dialog) which is available in the paint and modify the function as below

void CLoad_ImageDlg::OnPaint() 
    CPaintDC dc(this); // device context for painting
    HBITMAP m_Bmap;
    CRect rect;
    HDC memdc;
    CString path,m_ifilename1,m_ifilename2;


    for(int i=1;i<=path.GetLength();i++)
            path = path.Mid(0,i+10);
    m_ifilename1 = path + "<a href="file://"+"AK.bmp";GetClientRect(&rect);m_Bmap=(HBITMAP)::LoadImage(NULL,m_ifilename1,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);memdc=::CreateCompatibleDC(dc.m_hDC);::SelectObject(memdc,m_Bmap);BitBlt(dc.m_hDC,0,0,rect.right,rect.bottom,memdc,0,0,SRCCOPY/">\\"+"AK.bmp";





the above source will get the current path of application folder and load the image from that folad into dialog.

hope that this article helps you to link image into mfc dialog that you write.

This is my first article. If there is anything missing of explanation please mail me..

my email id :


June 16 - 2007


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer (Senior) VL Machine Vision Systems Pvt Ltd.,
India India
Bachelor degree in Computer Aplications.

- 9+ years experience in C, C++, and Visual C++.
- Obssessed in OOP style design and programming.
- Desigining and developing Industrial Auotmation Products.
- Designing and writing code to build a Image processing algorithms VC++.

The summary of my skills:
BASIC, COBOL, C++ code, VC++, MFC, , SQL Server, MS Access, VB, FoxPro, Desktop Tools.

You may also be interested in...


Comments and Discussions

QuestionNot working :( Pin
zet-jaro26-May-14 2:25
memberzet-jaro26-May-14 2:25 
QuestionIts not working.. Pin
gurunbr14-Apr-13 21:16
membergurunbr14-Apr-13 21:16 
GeneralMy vote of 2 Pin
gurunbr14-Apr-13 21:15
membergurunbr14-Apr-13 21:15 
GeneralMy vote of 5 Pin
Víctor Portela Romero8-Nov-12 3:35
memberVíctor Portela Romero8-Nov-12 3:35 
Generalmisc Pin
mc_cappy13-Jul-10 22:46
membermc_cappy13-Jul-10 22:46 
Generalresource leak Pin
ETA16-Jun-07 8:28
memberETA16-Jun-07 8:28 

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
Web02-2016 | 2.8.180515.1 | Last Updated 16 Jun 2007
Article Copyright 2007 by Khathar
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid