Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating CBitmap from byte values 1

Status
Not open for further replies.

cisotta

Programmer
Apr 23, 2004
9
0
0
IT
I'm trying to create a 256 graylevel CBitmap object, having the pixel values retrieved from a BYTE array.
I think I have to setup a BITMAPINFO structure, define the palette and then allocate the CBitmap object and set the bit values, but I can't manage with the palette definition stuff.

This is my code:


BITMAPINFO bmInfo;
BITMAPINFOHEADER &bmInfohdr = (BITMAPINFOHEADER)bmInfo.bmiHeader;

bmInfohdr.biSize = 40 + 255; //I think it's not of use
bmInfohdr.biWidth = x;
bmInfohdr.biHeight = y;
bmInfohdr.biPlanes=1;
bmInfohdr.biBitCount=8;
bmInfohdr.biCompression=0;
bmInfohdr.biSizeImage=0;
bmInfohdr.biXPelsPerMeter = 0;
bmInfohdr.biYPelsPerMeter = 0;
bmInfohdr.biClrUsed = 0;
bmInfohdr.biClrImportant = 0;

// should I allocate memory further than the
// bmColors[1]?? anyway the compiler gives an
// error for type mismatch!
//bmInfo.bmiColors = (RGBQUAD *)
malloc(sizeof(RGBQUAD) * 256);

// here I define the 256 graylevel palette
for (int i=0; i<256; i++)
{
bmInfo.bmiColors.rgbRed = i;
bmInfo.bmiColors.rgbGreen = i;
bmInfo.bmiColors.rgbBlue = i;
}


BYTE *matrix;
matrix = (BYTE*)malloc(size*sizeof(BYTE));
// here I put the BYTE values of the pixels

CDC *pdcDest = this->GetDC();

HBITMAP hBmp = CreateDIBitmap( pdcDest->m_hDC,
&bmInfohdr,
CBM_INIT,
matrix,
&bmInfo,
DIB_RGB_COLORS);
m_bmpBitmap.Attach( hBmp );


But when I load the m_bmpBitmap to screen, it's empty!
Can someone help me?
thanks,
francesco
 
One problem is on the very first line:

BITMAPINFO bmInfo;

This only leaves space for 1 entry in the palette. Instead, allocate a larger chunk of memory like this:

char chunk [sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 256];
BITMAPINFO &bmInfo = * (BITMAPINFO *) &chunk [0];

There might be other bugs in your code but this one practically leaped off the screen at me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top