Getting bitmap bits

I am trying to write text to a bitmap, then get access to the image buffer to do some pixel-level manipulations. I have been unable to get the buffer to fill using either the CBitmap::GetBitmap() or GetDIBits() functions - in both cases, the buffer remains null or empty. Does anyone have an example of functional code that does this, or have suggestions how to make it work? Thanks.
Who is Participating?
mahnoConnect With a Mentor Commented:
Hi DenMan,

If U still interesting to get the answer, I can tell U about method GetBitmapBits()

>Also...I have tried GetBitmapBits, and >the buffer it returns does not have an >obvious correlation with what the >contents should be. Is the standard >buffer returned from GetBitmapBits in >lookup-table format, or RGB format
>(i.e. 8-bit or 24-bit)?

U receive buffer, where:
each pixel is represented by word:

0rrr rrgg gggb bbbb

r - red
g - green
b - blue

(5bit to each color component)

The use of CBitmap::GetBitmapBits().
do you have CDib class?
this is best way
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

DenManAuthor Commented:
Is CDib available in MFC 4.2 (I'm using VC++ 5.0)?

Also...I have tried GetBitmapBits, and the buffer it returns does not have an obvious correlation with what the contents should be. Is the standard buffer returned from GetBitmapBits in lookup-table format, or RGB format (i.e. 8-bit or 24-bit)?
CDib class not from MFC, this class for work with *.BMP files, load in CBitmap object , access to RGB table ...
DenManAuthor Commented:
Thanks, I'll look into it.  I'm actually trying to create a small bitmap (with text in the image) that I can paste onto an existing image that is in raw data format. (I need the raw data to pass into image compression schemes I did not write).

Here is an example of code which seems like it should work, but no data is ever returned.

      CBitmap vBM;
      CClientDC dcScreen(this);
      CDC dcMem;

      vBM.CreateBitmap(20, 10, 1, 24, NULL);
      CBitmap *pOldBM = (CBitmap *) dcMem.SelectObject(&vBM);

      CBrush graybrush(RGB(128,128,128));
      CBrush whitebrush(RGB(255,255,255));

      dcMem.FillRect(CRect(0, 0, 20, 10), &graybrush);
      dcMem.FillRect(CRect(0, 0, 2, 4), &whitebrush);

      BITMAP vBMInfo;
      int vCount;
      char *pBuffer;
      vBM.GetObject(vCount, &vBMInfo); // here, count always returns as 0
      pBuffer = new char[vCount];
      vBM.GetBitmapBits(vCount, pBuffer);


I have also tried with using a pre-created char * array instead of NULL in the CreateBitmap, but when I do that the GetObject does not even return info to the BITMAP structure!
I have a game ...
In game I need a paint backround and some squares. With CDib Im load background and modufy this directly. This is simply way, and fast. Try .
DenManAuthor Commented:
I don't want to have to keep a bitmap on disk in order to be able to load it - I want to create it in memory. My fundamental goal is to place a block of text onto an existing image that is not in DIB or DDB format, so I was hoping to create a small bitmap, write the text onto the bitmap, extract the bits of the bitmap and copy them onto the existing image. If there is an easier way to do it, I'd love to hear it -- if I can't figure this out I'm going to have to create my own font and manually place characters onto the image, a task I don't relish.
All Courses

From novice to tech pro — start learning today.