Creating Windows DIB

For the basis of the code below, see The Other Version of ImageLock::read.
HBITMAP createDIB(HDC hdc, Image *inImage, const Rect& inRect)
{
    HBITMAP hBitmap = NULL;
    try
    {
        AutoPtr<Image> image = inImage;
        if(image->getPhotometric() != pmBGR8)
            image = ToSRGB(image, pmBGR8); // convert into sRGB

        BITMAPINFOHEADER bi;
        ::ZeroMemory(&bi, sizeof(bi));
        bi.biSize     = sizeof(bi);
        bi.biWidth    = (LONG)inRect.width;
        bi.biHeight   = (LONG)inRect.height;
        bi.biPlanes   = 1;
        bi.biBitCount = 24;
        bi.biCompression = BI_RGB;

        void *pvBits;
        hBitmap
            = ::CreateDIBSection(
                hdc, (CONST BITMAPINFO *)&bi, DIB_RGB_COLORS, &pvBits, NULL, 0);
        if(!hBitmap)
            return NULL;

        ssize_t rowStride = ((inImageRect.Width * bi.biBitCount / 8) + 3) & ~3;
        u8 *firstLine = (u8 *)pvBits + rowStride * (inRect.height - 1);

        AutoPtr<ImageLock> lock = image->lock();
        lock->read(inRect, firstLine, -rowStride);
        return hBitmap;
    }
    catch(...)
    {
        if(hBitmap) ::DeleteObject((HGDIOBJ)hBitmap);
        return NULL;
    }
}

See also:
Windows GDI - CreateDIBSection

Windows GDI - BITMAPINFOHEADER


This document is automatically generated using doxygen 1.5.4 at Fri Jun 27 18:22:54 2008.