Home | API | MFC | C++ | C | Previous | Next

Programming Windows API

Bitmaps

Windows supports two types of bitmap: device-independent (DIB) and device-dependent.(DDB). A device-independent bitmap is an external format, which allows bitmaps to be moved from one device to another. Device-dependent bitmaps are designed and optimised for use with a specific device (device-dependent) and hence are unsuitable for use in an environment for which they were not created. A typical example would be a bitmap created for video memory which is optimised for displaying screen output.

Creating and Loading Device-Independent Bitmaps

Device-independent bitmaps are typically created using an image editor. To load a bitmap for use in an application use the LoadImageMap() function. This function is best used to create color bitmaps. The prototype for this function is

HBITMAP LoadBitmap(HINSTANCE hInstance,LPCSTR lpBitmapName);

Where
hInstance – is a handle to the instance of the module whose executable file contains the bitmap to be loaded.
lpBitmapName – is a pointer to a null-terminated string that contains the name of the bitmap resource to be loaded. The MAKEINTRESOURCE macro can also be used to create this value.

If the function succeeds, the return value is the handle to the specified bitmap. If the function fails, the return value is NULL.

In order to then convert a DIB to a DDB, use the API functions CreateDIBitmap. 
https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createdibitmap

Creating Device Dependent Bitmaps

The API function CreateCompatibleBitmap() creates a bitmap compatible with the current device context handle. This function is best used for creating colour bitmaps.The prototype of this API function is 

HBITMAP CreateCompatibleBitmap (hdc, cx, cy) ;

Where
hdc is a handle to a device context
cx is the bitmap width, in pixels
cy is the bitmap height, in pixels. 

If the function succeeds, the return value is a handle to the compatible bitmap (DDB).  If the function fails, the return value is NULL.

In addition, a windows application can also create a device-dependent bit using the CreateBitmap API function. This function is best used for creating monochrome bitmaps. The syntax for this function is

HBITMAP CreateBitmap(nt nWidth,int nHeight,UINT nPlanes,UINT nBitCount,const VOID *lpBits);

where
nWidth - The bitmap width, in pixels.
nHeight - The bitmap height, in pixels.
nPlanes - The number of colour planes used by the device.
nBitCount - The number of bits required to identify the colour of a single pixel.
lpBits - Set the colours in a rectangle of pixels.

If the function succeeds, the return value is a handle to a bitmap.  If the function fails, the return value is NULL.

Displaying Bitmaps

In order to display a bitmap, first two device contexts must be declared: one will hold the current device context while the other will create a second copy device context that will be used to store the bitmap until it is ready to be drawn in the window. This copy device context is created using a call to API function CreatCompatibledDC().   A newly created or existing bitmap can then be selected into the copy device context using the SelectObject() API function and finally copied to the screen using the BitBlt() API function. The sequence of events is as follows -

DC=GetDC(hwnd);
memDC=CreateCompatibleDC(DC);
SelectObject(memDC,bitmap1);
BitBlt(DC,x.y,cx,cy, memdc,x1,y1,SRCCOPY);

The syntax for the CreateCompatibleDC function is

HDC CreateCompatibleDC(HDC hdc);

Where hdc is a handle to an existing DC. If this handle is NULL, the function creates a memory DC compatible with the application's current screen. If the function succeeds, the return value is the handle to a memory DC. If the function fails, the return value is NULL.

The syntax for this Bilblt copy function is

BOOL BitBlt(HDC hdc,int x,int y,int cx, int cy,HDC hdcSrc,int x1,int y1,DWORD rop); 

where
hdc - handle to the destination device context.
x - The x-coordinate upper-left corner of the destination rectangle.
y - The y-coordinate of the upper-left corner of the destination rectangle.
cx - The width of the source and destination rectangles.
cy - The height, in logical units, of the source and the destination rectangles.
HdcSrc - A handle to the source device context.
x1 - The x-coordinate of the upper-left corner of the source rectangle.
y1 - The y-coordinate of the upper-left corner of the source rectangle.
rop - A raster-operation code. Define how the colour data for the source rectangle will be combined with the destination rectangle. The 'vanilla' operating code SRCCOPY will simply copy the source rectangle directly onto the destination rectangle. For additional raster code information see the link below

Bltbmp returns zero if successful and non zero other otherwise.

When a bitmap a no longer needed it must be destroyed using the DeleteObject() API function.

For further information https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-bitblt

Repainting the Screen using Bitmaps

Using a bitmap to create a copy of the device context enables an application to keep a copy of the contents of the windows client area which can then be used repaint the display when the window receives a WM_PAINT message.

In the code section below, Windows creates a device context of the application window and a compatible device context, which acts as a virtual copy of the application display. The application then creates and fills this virtual copy with 200 random lines. Each time a repaint request is received the contents of the virtual windows are then copied to the screens device context by use of the BitBlt function.

Display Code
Download Code

Copying a Bitmap using the StretchBlt Function

The StretchBlt() function copies a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap to fit the dimensions of the destination rectangle.  In the code section below windows create a bitmap of the existing screen display and then copies it to application windows using StretchBlt stretching or compressing the screen bitmap copy as necessary. Clicking the left mouse button will initiate the screen copy action.

Display Code
Download Code

Home | API | MFC | C++ | C | Previous | Next
Creating a Simple Window | Common Elements | Data Types and Character Sets | The Device Context | Graphics Device Interface | Displaying Text | Displaying Graphics | Mapping Modes | Keyboard Input | Working with the Mouse | Menus | Child Windows | ScrollBar Control | The Dialog Box | Windows Message Box | Common Dialog Box | Bitmaps | Common Controls | Creating a Toolbar | Multiple Document Interface | Timers | DLL’s | Creating Custom Controls | Owner Drawn Controls | API Hooking and DLL Injection | File Management Functions | String Manipulation | System Information Functions