Retrieving a Bitmap of a Form

For a bitmap of the client area, simply use the TForm::GetFormImage member function.  To retrieve a bitmap of the entire Form, including the non-client areas, use the following apporach (compliments of Joe C. Hecht)...

KEYWORDS:  GetWindowDC, BitBlt, CreateCompatibleBitmap, LOGPALETTE
 


//---------------------------------------------------------------------------

void CreateFormBitmap(Graphics::TBitmap *Bitmap, TForm *Form)
{
    HDC HScreenDC = GetDC(0);
    Bitmap->Handle = CreateCompatibleBitmap(HScreenDC,
                                            Form->Width,
                                            Form->Height);

    int result = GetDeviceCaps(HScreenDC, RASTERCAPS);
    if (result & RC_PALETTE)
    {
        int palette_size = GetDeviceCaps(HScreenDC, SIZEPALETTE);
        if (palette_size == 256)
        {
            const size_t size = sizeof(LOGPALETTE) + 255 * sizeof(PALETTEENTRY);
            unsigned char* pBuffer = new unsigned char[size];

            LPLOGPALETTE lplogpal =
                reinterpret_cast<LPLOGPALETTE>(pBuffer);
            lplogpal->palVersion = 0x300;
            lplogpal->palNumEntries = 256;
            GetSystemPaletteEntries(HScreenDC, 0, 256,
                                    lplogpal->palPalEntry);

            Bitmap->Palette = CreatePalette(lplogpal);
            delete [] pBuffer;
        }
    }
    ReleaseDC(0, HScreenDC);

    HDC HWindowDC = GetWindowDC(Form->Handle);
    BitBlt(Bitmap->Canvas->Handle, 0, 0,
           Bitmap->Width, Bitmap->Height,
           HWindowDC, 0, 0,
           SRCCOPY);
    ReleaseDC(Form->Handle, HWindowDC);
}