Home

Bitmaps

 

Introduction 

A bitmap is a series of points (bits) arranged like a map so that, when put together, they produce a picture that can be written to, copied from, re-arranged, changed, manipulated, or stored as a a computer file. Bitmaps are used to display pictures on graphical applications, word processors, database files, or audience presentations. To display its product on a device such as a monitor or a printer, a bitmap holds some properties and follows a set of rules.

There are various types of bitmaps, based on the number of colors that the bitmap can display. First of all, a bitmap can be monochrome, in which case each pixel corresponds to 1 bit. A bitmap can also be colored. The number of colors that a bitmap can display is equal to 2 raised to the number of pits/pixel. For example, a simple bitmap uses only 4 pits/pixel or 4 bpp can handle only 24 = 16 colors. A more enhanced bitmap that requires 8 bpp can handle 28 = 256 colors. Bitmaps are divided in two categories that control their availability to display on a device.

A device-independent bitmap (DIB) is a bitmap that is designed to be loaded on any application or display on any device and produce the same visual effect. To make this possible, such a bitmap contains a table of colors that describes how the colors of the bitmap should be used on pixels when displaying it. The characteristics of a DIB are defined by the BITMAPINFO structure.

A device-dependent bitmap (DDB) is a bitmap created from the BITMAP structure using the dimensions of the bitmap.

Bitmap Creation

Unlike the other GDI tools, creating a bitmap usually involves more steps. For example, you may want to create a bitmap to display on a window. You may create another bitmap to paint a geometric area, in which case the bitmap would be used as a brush.

Before creating a bitmap as a GDI object, you should first have a bitmap. You can do this by defining an array of unsigned hexadecimal numbers. Such a bitmap can be used for a brush.

One way you can use a bitmap is to display a picture on a window. To do this, you must first have a picture resource. Although the image editors of both Borland C++ Builder and Microsoft Visual C++ are meant to help with regular application resources, they have some limitations. Nevertheless, once your bitmap is ready, call the LoadBitmap() function. Its syntax is:

HBITMAP LoadBitmap(HINSTANCE hInstance, LPCTSTR lpBitmapName);

The hInstance argument is the instance of the application that contains the bitmap you want to use

The lpBitmapName argument is the string that determines where the bitmap file is located. If you had imported the bitmap, you can use the MAKEINTRESOURCE macro to convert this string.

Before selecting the newly created bitmap object, allocate a block of computer memory that would hold the bitmap. You can then copy it to the actual device. This job can be taken care of by the CreateCompatibleDC() function. Its syntax is:

HBITMAP CreateCompatibleBitmap(HDC hdc, int nWidth, int nHeight);

This function takes a pointer to a device context. If it is successful, it returns TRUE or a non-zero value. If it is not, it returns FALSE or 0.

 
  1. For an example, start MSVC to create a new Win32 Project and name it Bitmap1

  2. Create it as a Windows Application and Empty Project

  3. Save this bitmap in your computer as exercise.bmp
     

  4. On the main menu, click either (MSVC 6) Insert -> Resource... or (MSVC .Net) Project -> Add Resource...

  5. Click the Import... button

  6. Locate the above picture from your computer and open or import it

  7. In the Properties window, change its ID to IDB_EXERCISING

  8. Save the resource. If you are using MSVC 6, save the resource script as Bitmap1.rc then add it to your project (Project -> Add To Project -> Files... Make sure you select the file with rc extension)

  9. Add a new C++ (Source) File named Exercise and implement it as follows:
     
    #include <windows.h>
    #include "Resource.h"
    
    HINSTANCE hInst;
    LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX  WndCls;
        static char szAppName[] = "BitmapIntro";
        MSG         Msg;
    
    	hInst       = hInstance;
        WndCls.cbSize        = sizeof(WndCls);
        WndCls.style         = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
        WndCls.lpfnWndProc   = WindProcedure;
        WndCls.cbClsExtra    = 0;
        WndCls.cbWndExtra    = 0;
        WndCls.hInstance     = hInst;
        WndCls.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        WndCls.hCursor       = LoadCursor(NULL, IDC_ARROW);
        WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        WndCls.lpszMenuName  = NULL;
        WndCls.lpszClassName = szAppName;
        WndCls.hIconSm       = LoadIcon(hInstance, IDI_APPLICATION);
        RegisterClassEx(&WndCls);
    
        CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                              szAppName,
                              "Bitmaps Fundamentals",
                              WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              NULL,
                              NULL,
                              hInstance,
                              NULL);
    
        while( GetMessage(&Msg, NULL, 0, 0) )
        {
            TranslateMessage(&Msg);
            DispatchMessage( &Msg);
        }
    
        return static_cast<int>(Msg.wParam);
    }
    
    LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg,
    			   WPARAM wParam, LPARAM lParam)
    {
        HDC hDC, MemDCExercising;
        PAINTSTRUCT Ps;
        HBITMAP bmpExercising;
    
        switch(Msg)
        {
    	case WM_DESTROY:
    	    PostQuitMessage(WM_QUIT);
    	    break;
    	case WM_PAINT:
    	    hDC = BeginPaint(hWnd, &Ps);
    
    	    // Load the bitmap from the resource
    	    bmpExercising = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_EXERCISING));
    	    // Create a memory device compatible with the above DC variable
    	    MemDCExercising = CreateCompatibleDC(hDC);
                 // Select the new bitmap
                 SelectObject(MemDCExercising, bmpExercising);
    
    	    // Copy the bits from the memory DC into the current dc
    	    BitBlt(hDC, 10, 10, 450, 400, MemDCExercising, 0, 0, SRCCOPY);
    
    	    // Restore the old bitmap
    	    DeleteDC(MemDCExercising);
    	    DeleteObject(bmpExercising);
    	    EndPaint(hWnd, &Ps);
    	    break;
    	default:
    	    return DefWindowProc(hWnd, Msg, wParam, lParam);
        }
        return 0;
    }

  10. Test the application
     

  11. Return to your programming environment

 

Previous Copyright © 2003-2015, FunctionX, Inc. Next