Archived

This topic is now archived and is closed to further replies.

Bitmaps and Bitcounts

This topic is 5619 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I''m having trouble with CreateBitmap and the bits per pixel. I am trying to generate a bitmap in the program and then display it on the screen. The code I have here displays black and white monochrome stripes, but if I try to adjust the bits per pixel in CreateBitmap() it won''t draw anything. I am shooting for 24-bit bitmaps but I can''t get any of them (8,16, etc.) working but the monochrome. I suspect I have to do something with the palette but I''m pretty ignorant of the effects of the various bitcounts and all that. Can anyone tell me why adjusting the bitcount causes the bitmap creation to fail? CPaintDC dc(this); // device context for painting CDC * dlgDC = this->GetDC(); CDC DC3; DC3.CreateCompatibleDC(NULL); BYTE BMPColors[10000]; for(int x=0;x<10000;x++) { BMPColors[x] = 0xaa; } CBitmap testBMP; // testMap->GetWorldSize() is the size of the window the bitmap is displayed on, and the size of the bitmap int flag = testBMP.CreateBitmap(testMap->GetWorldSize().x,testMap->GetWorldSize().y,1,1,BMPColors); DC3.SelectObject(testBMP); // simView is the location of the window for displaying the bitmap dlgDC->BitBlt(simView.x,simView.y, testMap->GetWorldSize().x,testMap->GetWorldSize().y,&DC3,0,0,SRCCOPY); Thanks

Share this post


Link to post
Share on other sites
somewhere in msdn says CreateCompatibleDC with NULL creates a monochrome bitmap as far as I remember.

so you should better try sth like that I suppose..
DC3.CreateCompatibleDC(dlgDC);

and also;
CreateCompatibleBitmap(dlgDC, width, height); instead of CreateBitmap..

Hope that helps..

Share this post


Link to post
Share on other sites
CreateCompatibleDC with NULL gives you the screen DC.

You probably want to call CreateCompatibleBitmap as a starting point, using the screen DC you got before, and make sure your logic is correct for building your bits. Then you can try the CreateBitmap, manually specifying the parameters it takes from the DC.

Share this post


Link to post
Share on other sites
To create the bitmap (DDB)

  
BitmapInfo.bmiHeader.biBitCount =
BitmapInfo.bmiHeader.biWidth = BitmapInfo.bmiHeader.biHeight = BitmapInfo.bmiHeader.biPlanes = 1; // required to be 1

BitmapInfo.bmiHeader.biCompression = BI_RGB;
BitmapInfo.bmiHeader.biSize = sizeof(BitmapInfo.bmiHeader);
BitmapInfo.bmiHeader.biSizeImage =

HDC hDC = GetDC(0);
Image = CreateDIBSection(hDC, &BitmapInfo,
DIB_RGB_COLORS, (void**)&ImageBuffer, NULL, 0);
::ReleaseDC(0,hDC);
hDC = 0;

ImageBuffer will be a pointer to a buffer that''s BitmapInfo.bmiHeader.biSizeImage big. BitmapInfo is a BITMAPINFO.

To display it...
BitBlt(hDC,0,0,bm.bmWidth,bm.bmHeight,hDCMem,0,0,SRCCOPY);
hdc where you want to bitblt to. hDCMem is the dc that''s bound to the bitmap.


Learned most of my bitmap stuff from here: http://www.codeguru.com/bitmap/index.shtml

Share this post


Link to post
Share on other sites
Yes, I left out some code. You need to fill it in yourself.
BitmapInfo.bmiHeader.biBitCount =
In your case, it should be 24.
BitmapInfo.bmiHeader.biWidth = your width
etc.
BitmapInfo.bmiHeader.biSizeImage = width*height*bit/8

Oh yeah, and CreateDIBSection allocates the buffer for ImageBuffer, so all you need to do is fill it in. Then call ::DeleteObject(ImageBuffer) to free the buffer once you''re done.

Share this post


Link to post
Share on other sites