SetPixel() and DrawText() not working in 1bit per pixel bitmap

Greg Wilson-Lindberg 126 Reputation points
2022-10-06T22:21:56.397+00:00

SetPixel() and DrawText() not working in 1bit per pixel bitmap

I'm trying to manipulate a 1 bit per pixel bitmap created with a CreateDIBSection() call.

I call SetPixel(hDC, x, x, RGB(255, 255, 255)) and SetPixel(hDC, x, x, RGB(0, 0, 0)) but I get no changes in the bitmap.

When I check the return value it is -1 which should indicate an error, but the GetLastError() call indicates that it succeeded with no errors.

Should I be using something other than setpixel() with a 1bbp bitmap? I would prefer not to have to write my own pixel setting routines.

I've also tried a DrawText() call, it returns a height of 16, but I get no text in the bitmap.

I have manually set bits in the bitmap and that is working correctly.

Regards,
Greg

Developer technologies | C++
Developer technologies | .NET | .NET CLI
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2022-10-07T03:30:11.41+00:00

    This test works for me with 1 bpp bitmap =>

                    int nWidth = 256, nHeight = 256;  
                    COLORREF clrBackground = RGB(0, 0, 255);  
                    COLORREF clrForeground = RGB(255, 255, 0);  
                    HBITMAP hBitmap = CreateMonochromeBitmap(nWidth, nHeight, clrBackground, clrForeground);  
                    HDC hDC = GetDC(NULL);  
                    HDC hDCMem = CreateCompatibleDC(hDC);                      
                    HBITMAP hBitmapOld = (HBITMAP)SelectObject(hDCMem, hBitmap);  
    
                    RECT rc = { 0, 0, nWidth, nHeight };  
                    SetTextColor(hDCMem, clrForeground);  
                    SetBkColor(hDCMem, clrBackground);  
                    DrawText(hDCMem, L"This is a test", -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);  
                    for (int i = 10; i <= nWidth - 10; i++)  
                        SetPixel(hDCMem, i, 10, clrForeground);  
    
                    // Test copy bitmap on screen  
                    BitBlt(hDC, 0, 0, nWidth, nHeight, hDCMem, 0, 0, SRCCOPY);  
    
                    SelectObject(hDCMem, hBitmapOld);  
                    DeleteDC(hDCMem);  
                    DeleteObject(hBitmap);  
                    ReleaseDC(NULL, hDC);  
    

    Utility function :

    HBITMAP CreateMonochromeBitmap(int cx, int cy, COLORREF clrBackground, COLORREF clrForeground)  
    {  
        struct  
        {  
            BITMAPINFOHEADER bi;  
            RGBQUAD bmiColors[2];  
        } dib = { 0 };  
        dib.bi.biSize = sizeof(dib.bi);  
        dib.bi.biWidth = cx;  
        dib.bi.biHeight = cy;  
        dib.bi.biPlanes = 1;  
        dib.bi.biBitCount = 1;  
        dib.bi.biCompression = BI_RGB;   
        dib.bmiColors[0].rgbBlue = GetBValue(clrBackground);  
        dib.bmiColors[0].rgbGreen = GetGValue(clrBackground);  
        dib.bmiColors[0].rgbRed = GetRValue(clrBackground);  
        dib.bmiColors[1].rgbBlue = GetBValue(clrForeground);  
        dib.bmiColors[1].rgbGreen = GetGValue(clrForeground);  
        dib.bmiColors[1].rgbRed = GetRValue(clrForeground);  
      
        HBITMAP hBitmap = NULL;  
        HDC hDC = CreateCompatibleDC(NULL);  
        if (hDC)  
        {  
            hBitmap = CreateDIBSection(hDC, (BITMAPINFO*)&dib, DIB_RGB_COLORS, NULL, NULL, 0);  
            DeleteDC(hDC);  
        }  
        return hBitmap;  
    }  
    

    248364-monochromebitmap.jpg

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.