I am writing a program to display values from a data file as an image. But I can only get a blue screen. Here is a small program resembling my code. Could you see what I have missed? I only changed OnDraw function.
Code:
void CColorDisplayView::OnDraw(CDC* pDC)
{
CColorDisplayDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
CRect rect;
GetClientRect(rect);
int nWidth = rect.Width();
int nHeight = rect.Height();
// Allocate a bitmap header. Windows takes care of freeing this memory.
BITMAPINFO * pbmiDIB = NULL;
pbmiDIB = (BITMAPINFO *) new BYTE[sizeof(BITMAPINFOHEADER) + 256*sizeof(WORD)];
if (pbmiDIB == NULL)
{
throw;
}
// Fill in the BITMAPINFOHEADER
pbmiDIB->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmiDIB->bmiHeader.biWidth = nWidth;
pbmiDIB->bmiHeader.biHeight = nHeight;
pbmiDIB->bmiHeader.biPlanes = 1;
pbmiDIB->bmiHeader.biBitCount = 8; // max 256 colors supported
pbmiDIB->bmiHeader.biCompression = BI_RGB;
pbmiDIB->bmiHeader.biSizeImage = 0;
pbmiDIB->bmiHeader.biXPelsPerMeter = 0;
pbmiDIB->bmiHeader.biYPelsPerMeter = 0;
pbmiDIB->bmiHeader.biClrUsed = 0;
pbmiDIB->bmiHeader.biClrImportant = 0;
CPalette cPalette;
BOOL success = cPalette.CreateHalftonePalette(pDC);
CPalette* pOldPal = pDC->SelectPalette(&cPalette, FALSE);
pDC->RealizePalette();
// Create our DIB. This call allocates the memory for our bitmap.
LPVOID pvBits = NULL;
HBITMAP hBitmap = CreateDIBSection(
pDC->GetSafeHdc(),
pbmiDIB,
DIB_PAL_COLORS,
(void **)&pvBits, // Here's where we put the image
NULL,
0);
LPBYTE pSensorValues = (LPBYTE) pvBits;
delete pbmiDIB;
pbmiDIB = NULL;
int k = 0;
for(int j = 0; j < nHeight; j++) {
for(int i = 0; i < nWidth; i++) {
*pSensorValues = k % 256;
pSensorValues++;
}
k++;
}
CDC memdc;
memdc.CreateCompatibleDC(pDC);
CBitmap* pOldBm = (CBitmap *) memdc.SelectObject(hBitmap);
BOOL bRet = pDC->BitBlt(
rect.left,
rect.top,
nWidth, nHeight,
&memdc, 0, 0, SRCCOPY );
}