Hi,

I can't seem to properly create a compatible bitmap in my MFC application. I'm trying to StretchBlt a byte buffer (with 8-bit greyscale pixel information) to a DC, but the only way to get it done is by taking a rather unconventional detour. Here's what I do:

Code:
        HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, "bitmap.bmp", IMAGE_BITMAP,
            0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);

        CBitmap cBmp;
        cBmp.Attach(hBmp);
        cBmp.SetBitmapBits(m_width*m_height, datap);

        CDC dcMem;
        dcMem.CreateCompatibleDC(dc);
        HBITMAP BitmapOld = (HBITMAP)SelectObject(dcMem, cBmp);
        dc->StretchBlt(xOffset, yOffset, m_width*rectSizeX, m_height*rectSizeY, &dcMem, 0, 0, m_width, m_height, SRCCOPY);
        dcMem.SelectObject(BitmapOld);
        dcMem.DeleteDC();
The file bitmap.bmp contains an uncompressed 8-bit greyscale image with exactly the same dimension of m_width*m_height. So far only by loading this one file first, attaching it to CBitmap cBmp and then calling SetBitmapBits with the pointer to my own byte buffer (datap), thus overwriting the bitmap information from the file, I can create a compatible bitmap out of my byte buffer that I can blit into the window. If I go like this

Code:
    BITMAPINFO *lpInfo = new BITMAPINFO;
    lpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    lpInfo->bmiHeader.biWidth = m_width;
    lpInfo->bmiHeader.biHeight = m_height;
    lpInfo->bmiHeader.biPlanes = 1;
    lpInfo->bmiHeader.biBitCount = 8;
    lpInfo->bmiHeader.biCompression = BI_RGB;
    lpInfo->bmiHeader.biSizeImage = m_width*m_height;
    lpInfo->bmiHeader.biClrUsed = 0;

    HBITMAP hBmp = (HBITMAP)::CreateDIBSection(0, lpInfo, DIB_RGB_COLORS, 0, 0, 0x0);
... and then do the rest mentioned above (Attach(), CreateCompatibleDC(), SelectObject(), StretchBlt() etc.), I see only a greenish version of the image I want to blit (but in right aspect ratio, stride, dimension etc.), and it keeps appearing and disappearing with a blurry spot all the time.

What am I doing wrong?

Mac