Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Drawing with Bitmaps

After you create a GDI bitmap object you can draw with it using a memory device context and then copy it to a destination device context. A bitmap is copied to a device using the Bit Block Transfer functions. These functions are commonly known as BitBlt (pronounced bit-blit) functions. These functions, such as BitBlt() and StretchBlt() are discussed in the next section.

As you read earlier in this chapter, you can create a compatible memory device context by using the CreateCompatibleDC() function.

When a memory device context is created, it is initialized with a monochrome bitmap. You then should select your custom bitmap into the device context (saving the pointer returned to the old bitmap) and perform the copy into the screen device context. After you finish, you must select the original monochrome bitmap back into the device context (thus deselecting your custom bitmap) before deleting the CBitmap object or calling DeleteObject() to destroy the GDI bitmap object.

Bitmap Copying

After you select a valid bitmap into a memory-based device context, you can use the BitBlt() device-context function to transfer all or part of the image onto a display-based (or printer-based) device context. BitBlt() lets you specify the source device context (your memory DC), the source coordinates to copy from (relative to the memory DC), the destination coordinates (relative to the destination DC), and the width and height describing the area to copy.

You also can specify a raster operation flag that can be used to invert the image during copying, merge the source with the destination image, just copy the source to the destination, or perform a number of other logical operations between the source and destination during the copy.

The StretchBlt() function lets you specify a source width and height, as well as a destination width and height. The image then expands or shrinks to fit the destination width and height. This method provides a fast and easy way to perform zoom operations. You can change the technique used to copy the image area by passing a flag value to the SetStretchBltMode() function. This lets you perform color-averaging stretch copies that give a better image representation (at the expense of speed) or allow a number of different stretching techniques.

You can use the MaskBlt() function to perform an image copy from a source device context through the holes in a mask provided by a monochrome bitmap into the destination device context.

The following code fragment shows how to create a memory device context, load a re source bitmap, and select it into the memory device context. Then you use StretchBlt() to stretch it so that it fills the view.

void CBitmapdemoView::OnDraw(CDC* pDC)
{
    // Create a compatible memory device context
    CDC dcMemory;
    dcMemory.CreateCompatibleDC(pDC);

    // Load and select the bitmap resource
    CBitmap bmImage;
    bmImage.LoadBitmap(IDB_TSTBITMAP);
    CBitmap *pbmOriginal = dcMemory.SelectObject(&bmImage);

    CRect rcClient;
    GetClientRect(rcClient);

    pDC->StretchBlt(0,0,rcClient.Width(),rcClient.Height(),
        &dcMemory,0,0,48,48,SRCCOPY);

    pDC->SelectObject(pbmOriginal);
}
						

Share ThisShare This

Informit Network