Writing to 8-bit memory bitmap

Writing to 8-bit memory bitmap

Post by Joseph M. » Fri, 01 Aug 2008 17:33:43

Here's a problem I've been wrestling with for almost a day.

I need to have an 8-bit bitmap created for later processing. It would seem obvious that
the right thing to do is to create an 8-bit bitmap, select it into a memory DC, do the
drawing, and then write the bitmap out.

We should live so long that something this obvious should work correctly.

First, it appears that it is impossible to create a memory DC that has a color depth other
than that of the display (even though the bitmap will never be written to the display). If
I CreateCompatibleDC, then CreateBitmap of an 8-bit bitmap, and SelectObject of the new
bitmap into the DC, it doesn't work (in spite of the documentation, that states that a
memory DC takes on the color depth of the bitmap that is selected into it...); the return
value from SelectObject is NULL (indicating failure) and GetCurrentBitmap/GetBitmap
reveals that the current bitmap in the DC is the 1x1 monochrome bitmap (so no selection

But if I CreateCompatibleBitmap, the bitmap takes on 24-bit color depth, which is
inconsitent with writing the bitmap to an AVI file (which is what I am doing); there is no
installed codec on my system that can handle 24-bit format (I'm running with WMP 11).

I'd prefer to write directly to the 8-bit bitmap than have to convert a 24-bit bitmap to 8
bits externally. The images are simple and use few colors, so I could do it, but I keep
thinking I'm missing something by not being able to create a memory DC I can write to
(there seems to be this horrible fixation that a memory DC needs to have the same color
depth as the display, although I cannot fathom why, and there is documentation that speaks
directly against this, but it is obviously lying)

Writing to 8-bit memory bitmap

Post by Michael Ph » Fri, 01 Aug 2008 22:07:34

> I'd prefer to write directly to the 8-bit bitmap than have to convert a

Create an empty 8bpp bitmap with CreateDIBSection and select it to a memory
device context.

You need to create a palette for the 8bpp bitmap and use SetDIBColorTable
before using any of the GDI drawing routines(e.g., BitBlt, etc.).

BitBlt and the like will do the color conversion for you, if necessary(i.e.,
32bpp, 16bpp, 24bpp drawing to 8bpp).


Writing to 8-bit memory bitmap

Post by Joseph M. » Fri, 01 Aug 2008 23:55:51

considered that, but the CreateDIBSection documentation is confusing. On the one hand,
it says

ppvBits [out] pointer to a variable that receives a pointer to the location of the DIB bit

which is what I would need, but then it says

hSection [in] ...if hSection is NULL, the system allocates memory for the DIB. In this
case, ... an application cannot later obtain a handle to this memory.

This seems to suggest that you cannot get hold of the memory that is used for the bitmap,
and at 5am I did not have enough functioning neurons to figure out exactly how to resolve
these two apparently conflicting statements. I have no idea why I would want a handle
when I clearly specified as NULL, or why I should be surprised if, later, I ask for it and
it tells me exactly that the handle I provided was NULL, but what does this have to do
with the ppvBits parameter? The documentation seems to be going out of its way to tell me
about something I clearly do not care about, and consequently the documentation doesn't
make any sense. If I give a handle of NULL for a file-mapping section, why would I have
the slightest interest in getting this NULL handle back? Why would I even care about
getting a handle to something that clearly is irrelevant?

Could it be that CreateDIBSection, if I give a NULL handle, creates a file-mapping object
for me? If so, why should I care about how it is implemented, and why does the
documentation suggest that I even *might* be interested in something that is a mere
implementation detail, completely beyond my concern?

That's when I posted the request and went to bed...

On Thu, 31 Jul 2008 09:07:34 -0400, "Michael Phillips, Jr." < XXXX@XXXXX.COM >


Writing to 8-bit memory bitmap

Post by Michael Ph » Sat, 02 Aug 2008 01:01:40

gt; ppvBits [out] pointer to a variable that receives a pointer to the

ppvBits is a pointer to the memory that was allocated with CreateDIBSection.
You may cache this pointer and use it later.

You can always retrieve the pointer to the DIB's memory by using:
DIBSECTION ds = {0};
GetObject(handletodib, sizeof(ds), &ds);

No. Internally, CreateDIBSection allocates memory using file mapping. You
can create your own file mapping object yourself, if you need to.

"Joseph M. Newcomer" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...