Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDI+ only draws monochrome on memory DC

Tags:

c++

gdi+

mfc

I'm trying to do some double buffering in an MFC application and trying to draw on the memory DC with GDI+. However, although I called CreateCompatibleDC(), I'm only getting a monochrome image. Here is the code:

CDC bufferDC;
CBitmap bufferBitmap;
bufferDC.CreateCompatibleDC(&dc);
bufferBitmap.CreateCompatibleBitmap(&bufferDC, 300, 300);
bufferDC.SelectObject(bufferBitmap);
Graphics g(bufferDC);
g.Clear(Color::Green);
dc.BitBlt(0, 0, 300, 300, &bufferDC, 0, 0, SRCCOPY);

Instead of a green patch, I see a rectangle of dithered black and white dots. I even tried to save the bitmap to disk after the g.Clear() call. It is indeed a 1-bit depth file.

Any ideas what went wrong? Thanks.

like image 533
Deling Ren Avatar asked Feb 16 '12 20:02

Deling Ren


2 Answers

A common mistake. A memory DC takes on the properties of the bitmap selected into it, no matter what compatibility it was created with. The default bitmap selected into a DC is monochrome. If you create a bitmap compatible with that DC, it will be monochrome too.

Create the bitmap to be compatible with the original DC, not the memory DC.

like image 176
Mark Ransom Avatar answered Sep 25 '22 00:09

Mark Ransom


Both the bitnmap and the bufferDC should be compatible with dc (whatever device it refers to), not the bitmap compatible ... with its own DC.

Try to give &dc to CreateCopmpatibleBitmap.

like image 33
Emilio Garavaglia Avatar answered Sep 26 '22 00:09

Emilio Garavaglia