Bitmaps

Bitmap is a file format to store image pixels in terms of bits in a file. It is a 2D array of bits (4bit, 8 bit, 16bit, 24bit) to store a 2D image. A graphical application can use a bitmap in tool-bars, buttons, menus and many other places. Image editor applications can create, manipulate (scale, scroll, rotate, and paint), and store images as files on a disk. Win32 or Linux graphical subsystem reads these bits one by one and creates pixels in the screen. Thus the entire picture is formed in the screen.

LoadBitmap

The LoadBitmap function loads the input bitmap resource from a module's executable file and returns the handle. This function has been superseded by the generic LoadImage function.

HBITMAP LoadBitmap(
  HINSTANCE hInstance,  /* handle to application instance */
  LPCTSTR lpBitmapName  /* address of bitmap resource name */
);

Parameters

  • hInstance - A valid handle to the instance of the module executable file that contains the bitmap to be loaded.
  • lpBitmapName - Pointer to a null-terminated input string that contains the name of the bitmap resource to be loaded. Alternate way to give this parameter is to provide the resource identifier in the low-order word and zero in the high-order word. MAKEINTRESOURCE macro can be used to create this id value.

Return Values

If the function succeeds, the return value is the handle to the specified bitmap. This handle to bitmap can be used further in various drawing/GDI functions. If the function fails, the return value is NULL.

Loading steps:

  • Call LoadBitmap with the resource file string name
  • Create a new device context which is compatible to current Window DC
  • Select this bitmap to this newly created memory device context
  • Copy bits from source of new device context to window device context
  • Free bitmap resource using DeleteObject
  • Free this new DC by calling DeleteDC

Source Code

#include <windows.h>

#define BMP_WIDTH 100
#define BMP_HEIGHT 40

HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT message, 
                         WPARAM wParam,
                         LPARAM lParam);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  WNDCLASS wc;
  MSG msg;
  HWND hWnd;
  ZeroMemory(&wc, sizeof(WNDCLASS));
  hInst = hInstance;
  wc.style           = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc     = (WNDPROC)WndProc;
  wc.hInstance       = hInstance;
  wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground   = (HBRUSH)(COLOR_BACKGROUND);
  wc.lpszClassName   = (LPCTSTR)"MyWin32Class";


  RegisterClass(&wc);

   hWnd = CreateWindow("MyBitmapWnd", "Bitmap loading demo!!", WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);
  while (GetMessage(&msg, NULL, 0, 0)) 
  {
    DispatchMessage(&msg);
  }
  return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  PAINTSTRUCT ps;
  HDC hdc, hmemdc;
  HBITMAP hbitmap;
  switch (message) 
  {
    case WM_PAINT:
    {
      hdc = BeginPaint(hWnd, &ps);
      GetClientRect(hWnd, &rt);
      hbitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAPLOGO));
      hmemdc = CreateCompatibleDC(hdc);
      SelectObject(hmemdc, hbitmap);
      BitBlt(hdc, (rt.right - rt.left)/2  - BMP_WIDTH/2,
            (rt.bottom - rt.top)/2  - BMP_HEIGHT/2,
             BMP_WIDTH, BMP_HEIGHT, hmemdc, 0, 0, SRCCOPY);
      DeleteObject(bitmap);
      DeleteDC(memdc);
      EndPaint(hWnd, &ps);
      break;
    }
    case WM_DESTROY:
      PostQuitMessage(0);
      break;
    default:
      return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

Output

LoadBitmap example demo application

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#