The help you needed in C, C++, MFC
Complete tutorials with free source code
PDF Print E-mail

How to draw a bitmap in a MFC dialog window

Loading a bitmap into our dialog window is not a difficult task. This article describes how to do that and provide you with an example project.

Create a dialog based MFC application. We have to add the bitmap we are going to display as a resource to our application. Open resource view of the left pane, right click on the project and select Add->Resource.

visual studio adding new resource

Select Bitmap as the resource type and click "Import". Then browse and select the bitmap file you want to add. Then it will be added to the project as a bitmap resource. You can see and edit it using visual studio.

We are going to load our bitmap to the main dialog of our application when we click a button in the dialog. So, first delete all the controls in the dialog and add a button to the dialog. Add the following code to the handler of that button.

void CdrawbitmapDlg::OnBnClickedButton1()
{
CRect r;CBitmap* m_bitmap;CDC dc, *pDC;BITMAP bmp;

m_bitmap = new CBitmap();m_bitmap->LoadBitmapW(IDB_BITMAP1);m_bitmap->GetBitmap(&bmp);

pDC = this->GetDC();dc.CreateCompatibleDC(pDC);dc.SelectObject(m_bitmap);
pDC->BitBlt(0, 0, bmp.bmWidth, bmp.bmHeight, &dc, 0, 0, SRCCOPY);
m_bitmap->DeleteObject();m_bitmap->Detach();}

Ok. Lets try to understand the code. We have to obtain what is called the device context of our window and draw in it. Then what we draw is displayed in the window. Similarly, if we need to print a document, what we have to do is obtain the device context of the printer and draw in that device context. That will print what we have drawn in the device context. We obtain the device context of the window by calling GetDC() function.

We use CBitmap to load and store the bitmap. LoadBitmap() function of CBitmap loads the bitmap specified by the parameter which is the resource identifier of the bitmap we have added as a resource. I have not changed the default resource identifier given by Visual Studio so still it is IDB_BITMAP1. If you have given a different name to the bitmap resource you added, you have to use that name as the argument to LoadBitmap().

Then, we use BitBlt() function to draw the bitmap in the device context. It has several parameters.

The first two parameters specify the upper left coordinates of the destination rectangle. You can change them and experiment to understand the use of them.

Next two parameters specify the width and height of the destination rectangle. We have given the actual width and height of the bitmap we are going to load as these parameters. We can't directly get the width and the height of the bitmap with CBitmap. So we have used BITMAP data type for that purpose.

The fifth argument is the device context from where we copy the bitmap.

The next two arguments are the upper left corner of the source bitmap. We have given 0 as both arguments so the actual upper left corner of the image is selected. You can give different positive values for this arguments (say 100, 100) and experiment to understand how it works.

Now you should be able to compile and run the application. You can download example project source code from here.