Sign in to follow this  

StretchDIBits funny colors

Recommended Posts

I am using StretchDIBits to paint a graph in a dialog but instead black and windowColor I get green and windowColor!!! Function looks like this CDC *pDC = m_container->GetDC(); pDC->SetStretchBltMode(HALFTONE); // tried other also BITMAPINFOHEADER bmiHeader; bmiHeader.biWidth=320; bmiHeader.biHeight=240; bmiHeader.biPlanes=1; bmiHeader.biBitCount=8; bmiHeader.biCompression=BI_RGB; bmiHeader.biSizeImage=320*240; byte MyBWImage[320*240]; for (i=1;i<320*100;i++) MyBWImage[i]=200; //graph drawing // depending on above number I get different colors: "200" green, "3" pale yellow... what are these colors and how do I set some normal ones? int aaa=StretchDIBits (pDC->GetSafeHdc(), 0,0,320,240, 0,0,320,240, MyBWImage, reinterpret_cast<LPBITMAPINFO>( &bmiHeader), DIB_RGB_COLORS, SRCCOPY); m_container->ReleaseDC();

Share this post

Link to post
Share on other sites
An 8 bit BMP has a palette associated with it, palette index 200 will be green, index 3 is pale yellow, etc. The BITMAPINFO struct has a palette in it, and you're supposed to pass one to StretchDIBits. However, because of the cast, you're reading into random memory - StretchDIBits expects an array of 256 RGBQUADs after the BITMAPINFOHEADER, so it tries to read that and happens to read green and pale yellow colours.

Use something like the following instead:

BYTE* pbyBuffer = new BYTE[sizeof(BITMAPINFO) + sizeof(RGBQUAD)*256];
BITMAPINFO* pBitmap = (BITMAPINFO*)pbyBuffer;

// Setup header

// Zero out palette
memset(pBitmap->bmiColors, 0, sizeof(RGBQUAD)*256);

// Set entry 200 to red
pBitmap->bmiColors[200].rgbRed = 255;

BYTE MyBWImage[320*240];
for (i=1;i<320*100;i++)

int aaa=StretchDIBits (pDC->GetSafeHdc(),

delete[] pbyBuffer;

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this