In discovering how to use gdi for a program I am writing, I stumbled upon some things which took me a while to figure out. So this is for the archives:

When a DC is first created (for example, by calling CreateCompatibleDC), it has a default 1-by-1 monochrome bitmap selected into it. So, if you subsequently call CreateCompatibleBitmap, you will get a monochrome bitmap. This isn't what you'd expect if you started with a color DC, created a compatible DC from that, and then asked for a compatible bitmap. To ensure that your bitmap is the same color format as the original DC, get the color information by calling GetDeviceCaps for BITSPIXEL and PLANES, and then use that information in a call to CreateBitmap:
BitsPixel = GetDeviceCaps(hDC, BITSPIXEL);
Planes = GetDeviceCaps(hDC, PLANES);
hbm = CreateBitmap(width, height, Planes, BitsPixel, NULL);

The second thing is that I had a huge memory leak and didn't know why. I tracked it down to a section that looked like everything was ok. I followed the guidelines of the apiRef.

After creating a DC, and a bitmap

Selectobject NewBitmap

color bitmap

copy to Hdc

Delete bitmap
Delete or release DC


Apparantly you must save the handle when you select the bitmap and then SelectObject oldBitmap before deleting everything.
If you don't, and you rerun the procedure, your memory just drops and drops and drops...

Hope this is all useful
Posted on 2002-02-07 23:32:30 by CsnWltr
Apparantly you must save the handle when you select the bitmap and then SelectObject oldBitmap before deleting everything. If you don't, and you rerun the procedure, your memory just drops and drops and drops...


That is an old trick designed to catch new players ;)
Don't forget that dc's are also created with default pens, brushes and fonts, so if you change any of those you also need to select the old one back in before destroying the dc.
Posted on 2002-02-08 02:51:39 by sluggy
Remarks for SelectObject:

This function returns the previously selected object of the specified type. An application should always replace a new object with the original, default object after it has finished drawing with the new object.

Not following this very quickly leads to BSODs on win9x.

As for the whole monochrome thing... I had that as well. But reading
PlatformSDK closely, you should see that it is documented... as
far as I remember, anyway. It's been more than a year I think :).
Posted on 2002-02-08 12:52:05 by f0dder