Jump to content
  • Advertisement
Sign in to follow this  
assainator

win32 draw to bitmap

This topic is 2537 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!