How To Draw A Bitmap On Computer Screen Using Microsoft Windows
How To Draw A Bitmap On Computer Screen Using Microsoft Windows
Bitmaps
A bitmap is an array of pixels, numbers which represents an image. Bitmap structures are
used to display images on most type of raster device (like monitor, printer etc.). A bitmap
contains information about the size of image i.e. rows and columns, pointer to the pixel
data, and the color information in the image. The color is represented by some number of
bits in an image. The bits per pixel is very important field to correctly draw the image on
some raster device. A 1-bit per pixel represents monochrome images that contain only
black and white colors. A 4-bits per pixel image can represent at the most 16 distinct
colors. Similarly 8-bits per pixel image can represent 256 distinct colors. An image with
24-bits per pixel represents the full range of colors i.e. 16.8 millions color. Such images
are called true-color images. When the bits per pixel are less than or equal to 8, a color
palette is used to store the number of colors. Microsoft Windows uses two type of bitmap
known as Device Dependent Bitmap (DDB) and Device Independent Bitmap (DIB). The
DDBs were in use before the launch of Windows 3.0. Now DIBs are frequently used to
represent an image.
As shown above, initialize the BITMAPINFO structure as per your image parameters.
This structure also contains the BITMAPINFOHEADER structure. The fields are well
understood and very clear from the parameter name itself. The important point to note
here while initializing this structure is that if bit count or bits per pixel is less than 8 then
bmiColors[] array must be initialized properly. The bmiColors[] represents the colors of
the palette when bit count is less than 8 otherwise it is not required. Secondly the biWidth
must me divisible by four. As internally it is represented by WORD and it must finish at
WORD boundaries. If your image width is not multiple of four, you must provide
padding while creating bitmap.
Step 2. Load the pixel data in a linear array of type unsigned char *lpBits.
int StretchDIBits(
HDC hdc, // handle to device context
int XDest, // x-coordinate of upper-left corner of dest. rectangle
int YDest, // y-coordinate of upper-left corner of dest. rectangle
int nDestWidth, // width of destination rectangle
int nDestHeight, // height of destination rectangle
int XSrc, // x-coordinate of upper-left corner of source rectangle
int YSrc, // y-coordinate of upper-left corner of source rectangle
int nSrcWidth, // width of source rectangle
int nSrcHeight, // height of source rectangle
CONST VOID *lpBits, // address of bitmap bits
CONST BITMAPINFO *lpBitsInfo, // address of bitmap data
UINT iUsage, // usage flags
DWORD dwRop // raster operation code
);