Jump to content
  • Advertisement
Sign in to follow this  

StretchDIBits funny colors

This topic is 3676 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

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=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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!