DenMan
asked on
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.
The use of CBitmap::GetBitmapBits().
do you have CDib class?
this is best way
this is best way
ASKER
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)?
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 ...
ftp://ftp.wegadesign.com/pub/wegabyte/cdib.zip
ftp://ftp.wegadesign.com/pub/wegabyte/cdib.zip
ASKER
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;
dcMem.CreateCompatibleDC(& dcScreen);
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);
dcMem.SelectObject(pOldBM) ;
vBM.DeleteObject();
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!
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;
dcMem.CreateCompatibleDC(&
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);
dcMem.SelectObject(pOldBM)
vBM.DeleteObject();
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!
hmmm
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 .
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 .
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.