用GetDIBits获取8位图像素点如何获取不了!求大神给代码

用GetDIBits获取8位图像素点怎么获取不了!!求大神给代码
贴代码如下,怎么遍历不了图片,而且发生断言:m_hobject!=NULL;然后还有严重的内存泄露。。求帮助
CBitmap m_bmp;
m_bmp.LoadBitmap("E:\bin\x64\2014_6_10_20_41_9_118.bmp");
BITMAP bm;
m_bmp.GetBitmap(&bm);
int nbyte = bm.bmBitsPixel/8;

BITMAPINFO bi;
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
bi.bmiHeader.biWidth = bm.bmWidth;    
bi.bmiHeader.biHeight = -bm.bmHeight;    
bi.bmiHeader.biPlanes = 1;    
bi.bmiHeader.biBitCount = 8;     //位深为8
bi.bmiHeader.biCompression = BI_RGB;     
bi.bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * nbyte;  
bi.bmiHeader.biXPelsPerMeter = 0;      
bi.bmiHeader.biYPelsPerMeter = 0; 
bi.bmiHeader.biClrUsed = 0;    
bi.bmiHeader.biClrImportant = 0; 
HDC hdc = ::GetDC(NULL); 

BYTE* pBits = (BYTE*)new BYTE[bm.bmWidth * bm.bmHeight * nbyte];   
HBITMAP hOldBmp = (HBITMAP)SelectObject(hdc,m_bmp);
BYTE picNum ;
int whiteFlag = 0;
::ZeroMemory(pBits, bm.bmWidth * bm.bmHeight * nbyte);
if (!::GetDIBits(hdc, m_bmp, 0, bm.bmHeight, pBits, &bi, DIB_RGB_COLORS))    
{    
delete pBits;    
pBits = NULL;    

myListNum.RemoveAll();

for(int i = 0;i <= 656;i++)
{
for(int j= 0;j <= 492;j++)
{
//bmWidthBytes:一行像素所占的字节数,一行像素的存储必须按word对齐,所以该值必须为2的倍数
BYTE r = pBits[i * nbyte + j * bm.bmWidthBytes + 2];  
BYTE g = pBits[i * nbyte + j * bm.bmWidthBytes + 1];  
BYTE b = pBits[i * nbyte + j * bm.bmWidthBytes + 0]; 
picNum = (int)((r*306 + g*601 + b*117)<<10); 
COLORREF color = RGB(b, g, r);
outfileName << "color" << color << endl;
//myListNum.AddTail = -1;
}
}

delete pBits;    
pBits = NULL; 
}
------解决方案--------------------
GetDIBits不是你这么用的,它是用来转换DDB到DIB的,比如GetDC()获取的DC中的HBITMAP或者CreateCompatibleBitmap创建的HBITMAP。你代码中m_bmp本来就是一个DIB了。
而且你贴的代码完全和你的错误没有关系嘛,断言明明说m_hobject!=NULL不对,你贴的代码里哪用到m_hobject啦?


------解决方案--------------------
// Create a 32 bit bitmap
m_ppvBits = new DWORD[iWidth*iHeight];
BITMAPINFO bih;

    // create DIB Section
    bih.bmiHeader.biSize          = sizeof(BITMAPINFOHEADER);
    bih.bmiHeader.biWidth         = iWidth; 
    bih.bmiHeader.biHeight        = iHeight; 
    bih.bmiHeader.biPlanes        = 1; 
    bih.bmiHeader.biBitCount      = 32; 
    bih.bmiHeader.biCompression   = BI_RGB; 
    bih.bmiHeader.biSizeImage     = 0; 
    bih.bmiHeader.biXPelsPerMeter = 0; 
    bih.bmiHeader.biYPelsPerMeter = 0; 
    bih.bmiHeader.biClrUsed       = 0; 
    bih.bmiHeader.biClrImportant  = 0; 


SetBitmap(&bih,m_ppvBits);

DWORD* pLine = (DWORD*)GetDIBits();


// Copy the bits into our 32 bit dib..
for(int i=0;i<iHeight;i++)
{
for(int j=0;j<iWidth;j++)
{
pLine[(i*iWidth)+j] = FixColorRef(tempDC.GetPixel(j,i));
}
}


inline COLORREF CDIBSectionLite::FixColorRef(COLORREF clr)
{
int r = GetRValue(clr);
int g = GetGValue(clr);
int b =  GetBValue(clr);

return RGB(b,g,r);
}

看看对你有没有帮助
------解决方案--------------------
HBITMAP hbmp=LoadImage(GetModuleHandle(0), "8_bit_bitmap.bmp", IMAGE_BITMAP, 0, 0, 0x2010);

BITMAP bmp;

GetObject(hbmp, sizeof(bmp), &bmp);

HDC dc = GetDC(0);
HDC mdc = CreateCompatibleDC(dc);

DeleteObject(SelectObject(mdc, hbmp));

RGBQUAD colors[256];

GetDIBColorTable(mdc, 0, 256, colors);

......

// bmp.bmBits指向pixels, 每字节内容是colors中的index, 颜色在colors中
...

DeleteDC(mdc);
DeleteObject(hbmp);
ReleaseDC(dc);