Moving DIBs To and From the Display

The DIB driver is a separate driver not associated with the display driver. Because of this, you cannot use BitBlt to move bitmaps between a DIB driver device context and a screen-device context. To copy from the screen-device context to a DIB-device context, use GetDIBits. To copy a DIB device context to the screen device context, use StretchDIBits.

You can maximize the speed of StretchDIBits in two ways. First, you might use a one-to-one mapping for the palette. Second, you might use the DIB_PAL_COLORS option to prevent color matching by GDI.

The following code fragment uses StretchDIBits to draw a CF_DIB to the physical display:

/* Draws a bitmap in CF_DIB format, using SetDIBits to device. Takes the
 * same parameters as BitBlt.
 */


BOOL DibBlt(HDC hdc, int x0, int y0, int dx, int dy, HANDLE hdib,
                        int x1, int y1, LONG rop, WORD wUsage)
{
        LPBITMAPINFOHEADER lpbi;
        LPSTR                pBuf;
        WORD                wNumColors;
        BOOL                f;

        if (!hdib)
                return PatBlt(hdc, x0, y0, dx, dy, rop);

        if (wUsage == 0)
                wUsage = DIB_RGB_COLORS;


        lpbi = (VOID FAR *)GlobalLock (hdib);

        if (!lpbi)
                return FALSE;

    wNumColors  = lpbi->biClrUsed ? lpbi->biClrUsed : 256;
    pBuf = (LPSTR)lpbi + lpbi->biSize + wNumColors * sizeof(RGBQUAD);
        f = StretchDIBits (hdc, x0, y0, dx, dy, x1, y1, dx, dy,
                                           pBuf, (LPBITMAPINFO)lpbi, wUsage, rop);

        GlobalUnlock(hdib);
        return f;
}