Saturday, November 5, 2011

Displaying a Bitmap of .Graphics Under Windows in C programming

We are familiar with drawing normal shapes on screen using a device context. How about drawing images on the screen? Windows does not permit displaying a bitmap image directly using a screen DC. This is because there might be color variations in the screen on which the bitmap was created and the screen on which it is being displayed. To account for such possibilities while displaying a bitmap Windows uses a different mechanism—a ‘Memory DC’
The way anything drawn using a screen DC goes to screen, anything drawn using a printer DC goes to a printer, similarly anything drawn using a memory DC goes to memory (RAM). But where in RAM—in the 1 x 1 pixel bitmap whose handle is present in memory DC. (Note that this handle was of little use In case of screen/printer DC). Thus if we attempt to draw a line using a memory DC it would end up on the 1 x 1 pixel bitmap. You would agree 1 x 1 is too small a place to draw even a small line. Hence we need to expand the size and color capability of this bitmap. How can this be done? Simple, just replace the handle of the 1 x 1 bitmap with the handle of a bigger and colored bitmap object. This is shown in Figure 18.7.





What purpose would just increasing the bitmap size/color would serve? Whatever we draw here would get drawn on the bitmap but would still not be visible. We can make it visible by simply copying the bitmap image (including what has been drawn on it) to the screen DC by using the API function BitBlt().Before transferring the image to the screen DC we need to make the memory DC compatible with the screen DC. Here making compatible means making certain adjustments in the contents of the memory DC structure. Looking at these values the screen device driver would suitably adjust the colors when the pixels in 

the bitmap of memory DC is transferred to screen DC using BitBlt( ) function.
Let us now take a look at the program that puts all these concepts in action. The program merely displays the image of a vulture in a window. Here is the code…
void OnPaint ( HWND hWnd )
{
HDC hdc ;
HBITMAP hbmp ;
HDC hmemdc ;
HGDIOBJ holdbmp ;
PAINTSTRUCT ps ;
hdc = BeginPaint ( hWnd, &ps ) ;
hbmp = LoadBitmap ( hInst, MAKEINTRESOURCE ( IDB_BITMAP1 ) ) ;
hmemdc = CreateCompatibleDC ( hdc ) ;
holdbmp = SelectObject ( hmemdc, hbmp ) ;
BitBlt ( hdc, 10, 20, 190, 220, hmemdc, 0, 0, SRCCOPY ) ;
EndPaint ( hWnd, &ps ) ;
SelectObject ( hmemdc, holdbmp ) ;
DeleteObject ( hbmp ) ;
DeleteDC ( hmemdc ) ;
}

On executing the program we get the window shown in Figure 18.7.








As usual we begin our drawing activity in OnPaint( ) by first getting the screen DC using the BeginPaint( ) function. Next we have loaded the vulture bitmap image in memory by calling the LoadBitmap( ) function. Its usage is similar to what we saw while creating a pattern brush in an earlier section of this chapter. Then we have created a memory device context and made its properties compatible with that of the screen DC. To do this we have called the API function CreateCompatibleDC( ). Note that we have passed the handle to the screen DC to this function. The function in turn returns the handle to the memory DC. After this we have selected the loaded bitmap into the memory DC. Lastly, we have performed a bit block transfer (a bit by bit copy) from memory DC to screen DC using the function BitBlt( ). As a result of this the vulture now appears in the window.
We have made the call to BitBlt( ) as shown below:
BitBlt ( hdc, 10, 20, 190, 220, hmemdc, 0, 0, SRCCOPY ) ;
Let us now understand its parameters. These are as under:
hdc – Handle to target DC where the bitmap is to be blitted
10, 20 – Position where the bitmap is to be blitted
190, 220 – Width and height of bitmap being blitted
0, 0 – Top left corner of the source image. If we give 10, 20 then the image from 10, 20 to bottom right corner of the bitmap would get blitted.
SRCCOPY – Specifies one of the raster-operation codes. These codes define how the color data for the source rectangle is to be combined with the color data for the destination rectangle to achieve the final color. SRCCOPY means that the pixel color of source should be copied onto the destination pixel of the target.

No comments:

Post a Comment