Draw bitmap to screen

Hi,
I am trying to draw a colour bitmap image to a window. I have a function (below) which at present does precisely nothing. I have searched through msdn and haven't found anything which helps me, so some pointers on what I'm doing wrong would be appreciated.
I'm working on a 32-bit Windows 7 machine, but I would strongly prefer to have this be backward compatible (beyond Windows 2000 is unescessary, though).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void thrDrawScene (unsigned char *drawing_bytes)
{
    if ((!thrScene.xSize) || (!thrScene.ySize)) return; // window x- and y-dimensions must both be nonzero, otherwise there is no point in continuing.
    BITMAPINFO info;
    info.bmiHeader.biSize = sizeof (BITMAPINFO);
    info.bmiHeader.biWidth = thrXScreenSize;
    info.bmiHeader.biHeight = thrYScreenSize;
    info.bmiHeader.biPlanes = 1;
    info.bmiHeader.biBitCount = BPP;
    info.bmiHeader.biCompression = BI_RGB;
    info.bmiHeader.biSizeImage = 0;
    info.bmiHeader.biClrUsed = 0;
    info.bmiHeader.biClrImportant = 0;
    unsigned char* pixels;  // the pointer that will point to pixels
    HBITMAP bmp = CreateDIBSection (hdc, &info, DIB_RGB_COLORS, (void**)(&pixels), NULL, NULL);
    HDC hdcMem = CreateCompatibleDC(hdc);
    int a = 0;
    while (a < 1000)
    {
        pixels[a] = 255;
        ++ a;
    }
    BitBlt(hdc, 0, 0, thrXScreenSize, thrYScreenSize, hdcMem, 0, 0, SRCCOPY);
    DeleteDC (hdcMem);
}


Thanks!
Last edited on
Try
info.bmiHeader.biSize = sizeof(info.bmiHeader);

instead of sizeof(BITMAPINFO)
Last edited on
Thanks, I didn't realise I was doing that wrong.

I just thought I'd add, when I compile, I get a warning that bmp is unused. I know this, and have tried to fiddle around with the code a bit, but I can't get it to do anything.
I have to tell you im not good with the DIB, HDC stuff, the only time i used it was for a bot for a certain game where chopping trees would enable me to get loads of money, because getting a single pixel with GetPixel was about as fast as getting the whole screen with GetDIBits

(92 woodcutting ftw, thanks c++!)
Last edited on
I have to tell you I m good wih he DBI, stuff,ec.
I've done it, and I'm posting the solution for anyone else who is in a similar situation. This is for a windows function which writes a load of bytes to the screen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void thrDrawScene (unsigned char *drawing_bytes)
{
    if ((!window.xSize) || (!window.ySize)) return;
    BITMAPINFO info;
    info.bmiHeader.biSize = sizeof (info.bmiHeader);
    info.bmiHeader.biWidth = window.xSize;
    info.bmiHeader.biHeight = window.ySize;
    info.bmiHeader.biPlanes = 1;
    info.bmiHeader.biBitCount = 24; // 24 bits per pixel - one unsigned char for each pixel
    info.bmiHeader.biCompression = BI_RGB;
    info.bmiHeader.biSizeImage = 0;
    info.bmiHeader.biClrUsed = 0;
    info.bmiHeader.biClrImportant = 0;
    HDC cDC = CreateCompatibleDC (hdc); // this is the GetDC (hwnd) where hwnd is the
                                        // handle of the window I want to write to
    HBITMAP hbmp = CreateCompatibleBitmap  (hdc, window.xSize, window.ySize);
    SetDIBits (hdc, hbmp, 0, window.ySize, drawing_bytes, &info, DIB_RGB_COLORS);
    hbmp = (HBITMAP) SelectObject (cDC, hbmp);
    BitBlt (hdc, 0, 0, window.xSize, window.ySize, cDC, 0, 0, SRCCOPY);
    DeleteObject (SelectObject(cDC, hbmp));
    DeleteDC (cDC);
}
Last edited on
Topic archived. No new replies allowed.