0% found this document useful (0 votes)
543 views

How To Draw A Bitmap On Computer Screen Using Microsoft Windows

The document discusses how to draw a bitmap on a computer screen using Microsoft Windows. It defines a bitmap as an array of pixels that represents an image, with each pixel indicated by a number representing its color. It explains that bitmaps can be device dependent (DDB), which uses the hardware palette and causes inconsistencies across devices, or device independent (DIB), which embeds color format and palette to maintain consistency. The summary provides steps to create a DIB, including initializing a BITMAPINFO header, loading pixel data, and using StretchDIBits to paste the bitmap onto the screen.

Uploaded by

sudhir1975
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
543 views

How To Draw A Bitmap On Computer Screen Using Microsoft Windows

The document discusses how to draw a bitmap on a computer screen using Microsoft Windows. It defines a bitmap as an array of pixels that represents an image, with each pixel indicated by a number representing its color. It explains that bitmaps can be device dependent (DDB), which uses the hardware palette and causes inconsistencies across devices, or device independent (DIB), which embeds color format and palette to maintain consistency. The summary provides steps to create a DIB, including initializing a BITMAPINFO header, loading pixel data, and using StretchDIBits to paste the bitmap onto the screen.

Uploaded by

sudhir1975
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Saturn Software Mills

Visit for more technical article http://www.saturnsoftmills.com

How to Draw a Bitmap on the computer Screen using


Microsoft Windows?
The graphics can be placed on top of your screen in two ways – raster graphics or vector
graphics. In Vector graphics mode, the mathematical functions or formulas are used to
represent a picture. The vector graphics can be drawn on the screen using these
mathematical formulas that can be scaled up to any level without any distortion. But in
raster graphics mode, the picture is represented by an array of pixel values or numbers
that represent the color at the specified location. The raster and vector mode are used or
suitable for different purposes. For example, the font library, geometrical objects etc. can
easily be represented in vector mode. But a picture of a human, scenic view etc. can
better be represented in raster mode. Here we will discuss the methods to create and paste
a bitmap (a bitmap is a raster array which stores the pixel or color information of a
picture) on the computer screen. Before starting the main discussion, let us see what is a
Bitmap?

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.

Device Dependent Bitmap (DDB)


The DDBs use the hardware palette to represent colors in the image. But the
inconsistency in the color palette was faced when the image generated on one device was
taken to another device. The colors were different because the hardware palette of source
and destination device was different. To avoid such problem, DIBs were invented.

Device Independent Bitmap (DIB)


DIB has solved the problem of interoperability of bitmaps among different raster device.
Now the DIB contains the information of the color format of the source device on which
the DIB is created. It also contains the complete palette information with it so that same
colors can be generated at the destination raster device. DIBs also contain some other
useful information that I don’t think is required to be discussed here.
Saturn Software Mills
Visit for more technical article http://www.saturnsoftmills.com

Steps required to create a DIB in Win32 environment

Step 1. Initialize the BITMAPINFO header.

typedef struct tagBITMAPINFO {


BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO;

typedef struct tagBITMAPINFOHEADER{


DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;

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.

Step 3. Paste the bitmap on the computer screen


Use the following function to paste the bitmap on the screen at the desired location.

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
);

You might also like