我现在要显示一幅黑白灰度的位图。程序如下所示,能够正常运行。
但问题是,程序中BITMAP bm = {0, iWidth, iHeight, iWidth *4, 1, 8*4};每个像素使用了4个字节,浪费严重,我想把它改成每个像素只占1个字节,可是改完之后,图像不能显示,不知道为什么?
请高手指点迷津。pBmpBuf原来是按照每个像素4字节存数据,这四个字节的值相同。
修改后,pBmpBuf每个像素1字节,BITMAP bm = {0, iWidth, iHeight, iWidth, 1, 8};/////////////////////////////////////////////
//pBmpBuf是位图的数据 CBitmap bmp;
CDC *pMemDC = NULL;
HDC hdcMem;//hdc, int iWidth, iHeight;
iWidth = iHeight = 200; hdcMem = CreateCompatibleDC(pDC->m_hDC);//(hdc);
pMemDC = new CDC;
pMemDC->Attach(hdcMem); BITMAP bm = {0, iWidth, iHeight, iWidth *4, 1, 8*4};
bm.bmBits = (LPVOID)pBmpBuf;
bmp.CreateBitmapIndirect(&bm); pMemDC->SelectObject(&bmp);
CSize size = CSize(iWidth, iHeight);
pDC->DPtoLP(&size);
pDC->SetStretchBltMode(COLORONCOLOR);
pDC->StretchBlt(0,0,iWidth,iHeight,pMemDC,0,0,iWidth,iHeight,SRCCOPY);
::DeleteDC(hdcMem);
::DeleteObject(&bmp);
pMemDC = NULL;
delete pMemDC;
但问题是,程序中BITMAP bm = {0, iWidth, iHeight, iWidth *4, 1, 8*4};每个像素使用了4个字节,浪费严重,我想把它改成每个像素只占1个字节,可是改完之后,图像不能显示,不知道为什么?
请高手指点迷津。pBmpBuf原来是按照每个像素4字节存数据,这四个字节的值相同。
修改后,pBmpBuf每个像素1字节,BITMAP bm = {0, iWidth, iHeight, iWidth, 1, 8};/////////////////////////////////////////////
//pBmpBuf是位图的数据 CBitmap bmp;
CDC *pMemDC = NULL;
HDC hdcMem;//hdc, int iWidth, iHeight;
iWidth = iHeight = 200; hdcMem = CreateCompatibleDC(pDC->m_hDC);//(hdc);
pMemDC = new CDC;
pMemDC->Attach(hdcMem); BITMAP bm = {0, iWidth, iHeight, iWidth *4, 1, 8*4};
bm.bmBits = (LPVOID)pBmpBuf;
bmp.CreateBitmapIndirect(&bm); pMemDC->SelectObject(&bmp);
CSize size = CSize(iWidth, iHeight);
pDC->DPtoLP(&size);
pDC->SetStretchBltMode(COLORONCOLOR);
pDC->StretchBlt(0,0,iWidth,iHeight,pMemDC,0,0,iWidth,iHeight,SRCCOPY);
::DeleteDC(hdcMem);
::DeleteObject(&bmp);
pMemDC = NULL;
delete pMemDC;
灵位 BITMAP bm = {0, iWidth, iHeight, iWidth *4, 1, 8*4};最后一个参数是不是有问题?
MSDN的解释:bmBitsPoints to the location of the bit values for the bitmap. The bmBits member must be a long pointer to an array of 1-byte values.
pBmpBuf是位图的数据,位图的数据是经过计算出来的,是黑白位图,不是彩色。
因此利用4个字节表示,肯定浪费很大。所以我希望改成用单字节表示像素。liu792730:加调色板之后就能够正确运行吗?如何加调色板?能不能详细说明一下?
谢谢了。