请看下面的部分代码:LPBYTE pData = new BYTE[400 * 300];
memset(pData, 240, 120000);::StretchDIBits(pDC->GetSafeHdc(),
0, 0,
400, 300,
0, 0,
400, 300,
pData,
pBitmapInfo, // BITMAPINFO结构,其中包括调色板
DIB_RGB_COLORS,
SRCCOPY);以上是简化代码,pBitmapInfo的数据完整,调色板每个RGB分量相等且为0-255。按预定结果,应该显示出来的是一个400*300的,颜色为RGB(240,240,240)的灰色方块。
但实际显示出的方块却为RGB(247,243,247)有点粉红色的方块。请问,这是为什么?
memset(pData, 240, 120000);::StretchDIBits(pDC->GetSafeHdc(),
0, 0,
400, 300,
0, 0,
400, 300,
pData,
pBitmapInfo, // BITMAPINFO结构,其中包括调色板
DIB_RGB_COLORS,
SRCCOPY);以上是简化代码,pBitmapInfo的数据完整,调色板每个RGB分量相等且为0-255。按预定结果,应该显示出来的是一个400*300的,颜色为RGB(240,240,240)的灰色方块。
但实际显示出的方块却为RGB(247,243,247)有点粉红色的方块。请问,这是为什么?
{
pBitmapInfo->bmiColors[i].rgbRed = i;
pBitmapInfo->bmiColors[i].rgbGreen = i;
pBitmapInfo->bmiColors[i].rgbBlue = i;
pBitmapInfo->bmiColors[i].rgbReserved = 0;
}
{
// 位图信息头定义
LPBITMAPINFO pBitmapInfo;
pBitmapInfo = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
// 位图信息头赋值
pBitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFO);
pBitmapInfo->bmiHeader.biWidth = 400;
pBitmapInfo->bmiHeader.biHeight = 300;
pBitmapInfo->bmiHeader.biPlanes = 1;
pBitmapInfo->bmiHeader.biBitCount = 8;
pBitmapInfo->bmiHeader.biCompression = 0;
pBitmapInfo->bmiHeader.biSizeImage = 120000;
pBitmapInfo->bmiHeader.biXPelsPerMeter = 2835;
pBitmapInfo->bmiHeader.biYPelsPerMeter = 2835;
pBitmapInfo->bmiHeader.biClrUsed = 256;
pBitmapInfo->bmiHeader.biClrImportant = 256;
// 位图调色板赋值
for (int i = 0; i < 256; i ++)
{
pBitmapInfo->bmiColors[i].rgbRed = i;
pBitmapInfo->bmiColors[i].rgbGreen = i;
pBitmapInfo->bmiColors[i].rgbBlue = i;
pBitmapInfo->bmiColors[i].rgbReserved = 0;
} // 位图数据定义与赋值,将400*300的区域全部赋为m_nColors(0-255)
LPBYTE pData = new BYTE[400 * 300];
memset(pData, m_nColors, 120000); // 逻辑调色板定义
LOGPALETTE * pPal = (LOGPALETTE*) new BYTE[2 * sizeof(WORD) + 256 * sizeof(PALETTEENTRY)];
pPal->palVersion = 0x300;
pPal->palNumEntries = 256;
for (i = 0; i < pPal->palNumEntries; i ++)
{
pPal->palPalEntry[i].peRed = pBitmapInfo->bmiColors[i].rgbRed;
pPal->palPalEntry[i].peGreen = pBitmapInfo->bmiColors[i].rgbGreen;
pPal->palPalEntry[i].peBlue = pBitmapInfo->bmiColors[i].rgbBlue;
pPal->palPalEntry[i].peFlags = 0;
}
CPalette * m_pPalette;
m_pPalette = new CPalette;
m_pPalette->CreatePalette(pPal);
delete []pPal; // 调色板实现
CPalette * pOldPal = NULL;
pOldPal = pDC->SelectPalette(m_pPalette, TRUE);
pDC->RealizePalette(); // 位图显示
::StretchDIBits(pDC->GetSafeHdc(),
0, 0,
400, 300,
0, 0,
400, 300,
pData,
pBitmapInfo,
DIB_RGB_COLORS,
//DIB_PAL_COLORS,
SRCCOPY); if (pOldPal)
pDC->SelectPalette(pOldPal, FALSE); CString str;
str.Format("Red : %d",pBitmapInfo->bmiColors[m_nColors].rgbRed);
pDC->TextOut(10, 10, str);
str.Format("Green : %d",pBitmapInfo->bmiColors[m_nColors].rgbGreen);
pDC->TextOut(10, 30, str);
str.Format("Blue : %d",pBitmapInfo->bmiColors[m_nColors].rgbBlue);
pDC->TextOut(10, 50, str); delete []pData;
delete pBitmapInfo;
delete m_pPalette;
}// 右键点击响应函数
void CMyTestView::OnRButtonDown(UINT nFlags, CPoint point)
{
m_nColors -= 5; // m_nColors为CMyTestView的成员函数,初始值为255
if (m_nColors < 0)
m_nColors = 255;
Invalidate();
}