win32 draw to bitmap

Started by
2 comments, last by Amr0 12 years, 3 months ago
Hello everyone,

I would like to be able to create an empty bitmap, draw on that bitmap and then draw that bitmap to a window. All I could find on the internet was how to draw an existing bitmap but I want to create a new one (with a size I specify), modify that bitmap finally draw that new bitmap.

I have found out it has something to do with CreateCompatibleDC and CreateCompatibleBitmap but I have no idea how to use them. Can somebody please show me a working example or instruct in what order I should do things?
"What? It disintegrated. By definition, it cannot be fixed." - Gru - Dispicable me

"Dude, the world is only limited by your imagination" - Me

Advertisement
Basically you do it like the following ...


// Create an offscreen bitmap and draw into it...
HBITMAP CreateOffscreenBmp(int wd, int hgt) {
// Get a device context to the screen.
HDC hdcScreen = GetDC(NULL);

// Create a device context
HDC hdcBmp = CreateCompatibleDC( hdcScreen );

// Create a bitmap and attach it to the device context we created above...
HBITMAP bmp = CreateCompatibleBitmap( hdcScreen, wd, hgt );
HBITMAP hbmOld = static_cast<HBITMAP>( SelectObject(hdcBmp, bmp) );

// Now, you can draw into bmp using the device context hdcBmp...
RECT r = {0, 0, wd, hgt};
FillRect( hdcBmp, &r, static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)) );
// etc...

// Clean up the GDI objects we've created.
SelectObject( hdcBmp, hbmOld);
DeleteDC( hdcBmp );
ReleaseDC( NULL, hdcScreen);

return bmp;
}

// to paint the bitmap into a window do something like the following using the HDC you get from BeginPaint()
// as hdcDst.
void BlitToHdc( HDC hdcDst, HBITMAP hbmSrc, int x, int y, int wd, int hgt) {
HDC hdcScreen = GetDC(NULL);
HDC hdcSrc = CreateCompatibleDC( hdcScreen );
HBITMAP hbmOld = static_cast<HBITMAP>( SelectObject(hdcSrc, hbmSrc) );

BitBlt( hdcDst, x, y, wd, hgt, hdcSrc, 0, 0, SRCCOPY );

SelectObject( hdcSrc, hbmOld);
DeleteDC( hdcSrc );
ReleaseDC( NULL, hdcScreen);
}

you can keep the HDC's around if you want rather than re-creating them every time and can use 0's instead of hdcScreen in the CreateCompatibleXXX calls, I think -- I always do it like the above because I think it is clearer this way.

All of this is a lot easier using GDI+ I would imagine; however, I can't write the GDI+ version off the top of my head. If you are just learning GDI programming, you might want to consider learning GDI+ instead.
Thanks a lot, I got it to work.

EDIT: I just looked at GDI+ and it indeed seems a lot simpler to use.
"What? It disintegrated. By definition, it cannot be fixed." - Gru - Dispicable me

"Dude, the world is only limited by your imagination" - Me

I wrote a GDI memory bitmap class which encapsulates this some time ago. I needed per-pixel access for use with a toy CPU-only ray tracer. I bundled the class with a simple win32 application that demonstrates its usage. I know jwezorek has already answered but maybe you'll find it useful as well.

This topic is closed to new replies.

Advertisement