CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2011

    Question [Need Help] SetDIBits with manually set bitmap

    Hey guys, I've been working with my software rasterizer but stuck with painting hand made bitmap onto the screen. The idea is:
    1. Create compatible DC and bitmap with current DC
    2. Set up BITMAPINFO struct
    3. Set the buffer for pixel
    4. SetDIBits with the BITMAPINFO and buffer into the compatible DC
    5. BitBlt compatible DC to current DC

    Am I wrong with all these steps? Here is part of the code: (32 bits)

    HDC hdcReal, hdcMem;
    HBITMAP hBitmap;
    BITMAPINFO bitmapInfo;
    BYTE *buffer;
    RECT rect;

    hdcReal = GetDC(hwnd);
    hdcMem = GetCompatibleDC(hdcReal);
    hBitmap = GetCompatibleBitmap(hdcReal, SCREEN_WIDTH, SCREEN_HEIGHT);

    memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
    pHeader = &(bitmapInfo.bmiHeader);
    pHeader->biSize = sizeof(BITMAPINFOHEADER);
    pHeader->biWidth = SCREEN_WIDTH;
    pHeader->biHeight = SCREEN_HEIGHT;
    pHeader->biPlanes = 1;
    pHeader->biBitCount = 32;
    pHeader->biCompression = BI_RGB;
    /* copy it from msdn, deal with alignment */
    pHeader->biSizeImage = ((pHeader->biWidth * 32 + 31) & ~31) / 8 * pHeader->biHeight;

    menset(buffer, 255, SCREEN_WIDTH * SCREEN_HEIGHT * COLOR_BYTES);

    SetDIBits(hdcMem, hBitmap, 0, SCREEN_HEIGHT, buffer, &bitmapInfo, DIB_RGB_COLORS);
    BitBlt(hdcReal, 0, 0, SCREEN_WIDHT, SCREEN_HEIGHT, hdcMem, 0, 0, SRCCOPY);

    GetClientRect(hwnd, &rect);
    ValidateRect(hwnd, &rect);

    ReleaseDC(hwnd, hdcReal);

    I suppose it would display a white screen, cause the buffer is fill with 255, but what I got is just a blank black screen, what's wrong with my code? Any reply would be appreciated!

  2. #2
    Join Date
    Nov 2003

    Re: [Need Help] SetDIBits with manually set bitmap

    Why do you show uncompilable code?
    There are several typos in that code before it even compiles...

    Anyway, you need to select hBitmap into hdcMem before you can use it. Something like:
    SelectObject(hdcMem, hBitmap);
    Don't forget to store the result of SelectObject and restore it when you are finished.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.