Render straight into a bitmap

I’m trying to render straight into a bitmap. I’ve seen several examples on the net, but they all use a window to get the device context that is required to make a rendering context. To avoid having to make a window, which is really what I want, I use the device context from a bitmap-object (I’m writing in C#, by the way) to make a rendering context. I’m using PFD_DRAW_TO_BITMAP instead of PFD_DRAW_TO_WINDOW in the pixelformatdescriptor and no double buffering.
Now, when I call glClearColor the background of the bitmap does get the right colour. :slight_smile: So I guess the rendering context is valid. The strange thing is that any attempt to draw primitives does not show in the bitmap. :confused: (It’s not that I’m drawing outside the view or something; I’ve checked that about a million times. The code works fine when I use a device context from a window.)
Does anyone have a clue why I can’t seem to draw? Or perpaps someone has a different approach to render straight into a bitmap without the need for a window device context? Any help is welcome.


The problem of PFD_DRAW_TO_BITMAP is that you end up with MS OpenGL 1.1 software implementation. That does not explain why you can’t see your primitive, but may be a start.

The better way I have heard (but more involved) is about creating an hidden window to get a DC then create and use a pbuffer.

Do a search here, this have been discussed several times, maybe on ‘advanced’ forum too.

try this CreateCompatibleDC() to create memory dc from desktop dc that you can get desktop dc by GetDC(NULL);

just set the windows bps to what you want.

notify me when you checked ,plz.

try this CreateCompatibleDC() to create memory dc from desktop dc
You’re right. Using CreateCompatibleDC(NULL) gets you a device context compatible with the current screen. You can then make a DIB with CreateDIBSection (or, alternatively, make a DDB with CreateBitmap). Then select the handle to this memory bitmap into the device context with SelectObject.
However, whether you use CreateCompatibleDC or create an invisible window, in both cases you start with a screen device context. My original question, which I may not have put across clearly enough, was whether you can somehow get a device context purely based on an existing bitmap. So, not get a screen DC first and then make a compatible bitmap, but generate a bitmap first and then make a compatible device context to select it in. I ment that I have not seen any examples of that. Maybe it’s because it is not possible.

Thanx for the response through,

FYI: I neglected to say why I don’t want to use a screen DC. The reason is because you’re bound to the colourdepth of your display (usually either 16-bit or 32-bit), whereas I’d like to to able to choose it.



I don’t know why you are not getting primitives, perhaps it’s just some simple oversight… would need to see your code.

But, even if you use:

hdc = CreateCompatibleDC(NULL)

you are not locked to the bit-depth of your current display settings, although, you are perhaps limited by the capabilities of your display driver.

You can get a working example from:

This works for bitmaps of 16, 24, and 32 bits regardless of whether the display is set to 8, 16, 24, or 32 bits. It is hard coded for a context with a depth buffer, but that is easy enough to change.


This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.