Copying an Image from a Display Screen to a Bit Map

To copy an image from a display screen to a bit map:

The following figure demonstrates these steps.

    HDC hdcMem;
    PSZ pszData[4] = { "Display", NULL, NULL, NULL };
    HPS hpsMem, hps;
    HAB hab;
    SIZEL sizlPage = {0, 0};
    BITMAPINFOHEADER2 bmp;
    PBITMAPINFO2 pbmi;
    HBITMAP hbm;
    SHORT sWidth = 128, sHeight = 128;
    POINTL aptl[3];
    LONG alData[2];

    /*
     * Create the memory device context and presentation space so they
     * are compatible with the screen device context and presentation space.
     */

    hdcMem = DevOpenDC(hab, OD_MEMORY, "*", 4,
        (PDEVOPENDATA) pszData, NULLHANDLE);

    hpsMem = GpiCreatePS(hab, hdcMem, &sizlPage,
        PU_PELS | GPIA_ASSOC | GPIT_MICRO);

    /* Determine the device's plane/bit-count format. */

    GpiQueryDeviceBitmapFormats(hpsMem, 2, alData);

    /*
     * Load the BITMAPINFOHEADER2 and BITMAPINFO2 structures. The sWidth and
     * sHeight fields specify the width and height of the destination
     * rectangle.
     */

    bmp.cbFix = (ULONG) sizeof(BITMAPINFOHEADER2);
    bmp.cx = sWidth;
    bmp.cy = sHeight;
    bmp.cPlanes = alData[0];
    bmp.cBitCount = alData[1];
    bmp.ulCompression = BCA_UNCOMP;
    bmp.cbImage = (((sWidth *
        (1 << bmp.cPlanes) * (1 << bmp.cBitCount)) + 31) / 32) * sHeight;
    bmp.cxResolution = 70;
    bmp.cyResolution = 70;
    bmp.cclrUsed = 2;
    bmp.cclrImportant = 0;
    bmp.usUnits = BRU_METRIC;
    bmp.usReserved = 0;
    bmp.usRecording = BRA_BOTTOMUP;
    bmp.usRendering = BRH_NOTHALFTONED;
    bmp.cSize1 = 0;
    bmp.cSize2 = 0;
    bmp.ulColorEncoding = BCE_RGB;
    bmp.ulIdentifier = 0;


    DosAllocMem((PPVOID)&pbmi, sizeof(BITMAPINFO2) +
        (sizeof(RGB2) * (1 << bmp.cPlanes) * (1 << bmp.cBitCount)),
        PAG_COMMIT | PAG_READ | PAG_WRITE);

    pbmi->cbFix = bmp.cbFix;
    pbmi->cx = bmp.cx;
    pbmi->cy = bmp.cy;
    pbmi->cPlanes = bmp.cPlanes;
    pbmi->cBitCount = bmp.cBitCount;
    pbmi->ulCompression = BCA_UNCOMP;
    pbmi->cbImage = ((sWidth+31)/32) * sHeight;
    pbmi->cxResolution = 70;
    pbmi->cyResolution = 70;
    pbmi->cclrUsed = 2;
    pbmi->cclrImportant = 0;
    pbmi->usUnits = BRU_METRIC;
    pbmi->usReserved = 0;
    pbmi->usRecording = BRA_BOTTOMUP;
    pbmi->usRendering = BRH_NOTHALFTONED;
    pbmi->cSize1 = 0;
    pbmi->cSize2 = 0;
    pbmi->ulColorEncoding = BCE_RGB;
    pbmi->ulIdentifier = 0;

    /* Create a bit map that is compatible with the display.            */

    hbm = GpiCreateBitmap(hpsMem, &bmp, FALSE, NULL, pbmi);

    /* Associate the bit map and the memory presentation space.         */

    GpiSetBitmap(hpsMem, hbm);

    /* Copy the screen to the bit map.                                  */

    aptl[0].x = 0;       /* Lower-left corner of destination rectangle  */
    aptl[0].y = 0;       /* Lower-left corner of destination rectangle  */
    aptl[1].x = sWidth;  /* Upper-right corner of destination rectangle */
    aptl[1].y = sHeight; /* Upper-right corner of destination rectangle */
    aptl[2].x = 0;       /* Lower-left corner of source rectangle       */
    aptl[2].y = 0;       /* Lower-left corner of source rectangle       */

    hps = WinGetScreenPS(HWND_DESKTOP);

    GpiBitBlt(hpsMem, hps,
        sizeof(aptl) / sizeof(POINTL), /* Number of points in aptl      */
        aptl, ROP_SRCCOPY, BBO_IGNORE);

    WinReleasePS(hps);


[Back: Using Bit Maps]
[Next: Scaling and Drawing a Bit-Map Image]