求如何从HBITMAP中取出里面的位图数据,例如 GetBits(HBITMAP hBitmap,void * pBuf,int nLen),参数为一个HBITMAP,如何将数据读到pBuf中。这里贴出我的部分代码(不能正常运行)
HPALETTE hPal = (HPALETTE)GetStockObject(DEFAULT_PALETTE),hOldPal = NULL;
if (hPal)
{
hOldPal = SelectPalette(m_hMemDC, (HPALETTE)hPal, FALSE);
RealizePalette(m_hMemDC);
}
int nRet = GetDIBits(m_hMemDC, m_hMemBMP,0,m_nHeight,m_pBits,&infBitmap,DIB_RGB_COLORS);
if (hOldPal)
SelectPalette(m_hMemDC,hOldPal,FALSE);
---------------
求高手解

解决方案 »

  1.   

    info 结构必须填写,否则无法取得数据
    ============================================================================
    提问题时标题要简明扼要地说明问题内容,切忌使用"急","求救"之类不能说明问题的标题
    遇到问题可以给我发消息,给我发信息时请附带原帖地址
    http://alphasun.icpcn.com/alphasun/index.htm
    http://alphasun.icpcn.com/alphasun/YaCompile/index.htm YaCompile批量自动编译VC工程
    DocWizard C++程序文档自动生成工具 | Wave OpenGL | HttpProxy | AjaxParser词法分析
      

  2.   

    下面函数展示了如何使用GetDIBitsvoid Draw(HDC hDC,HBITMAP hBmp,double dScaleX,double dScaleY,int iX,int iY,int iWidth=0,int iLength=0)
    {
    HPALETTE hPal;
    BITMAP bm;
    BITMAPINFOHEADER bi;
    LPBITMAPINFOHEADER  lpbi;
    DWORD dwLen;
    HANDLE hDIB;
    HANDLE handle;
    HDC  hDC1;
    if(GetDeviceCaps(hDC,RASTERCAPS) & RC_PALETTE )
    {
    UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
    LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
    pLP->palVersion = 0x300;
    pLP->palNumEntries =GetSystemPaletteEntries( hDC, 0, 255, pLP->palPalEntry );
    hPal=CreatePalette(pLP );
    delete[] pLP;
    }
    if (hPal==NULL) hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
    ::GetObject(hBmp,sizeof(bm),(LPSTR)&bm);
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bm.bmWidth;
    bi.biHeight  = bm.bmHeight;
    bi.biPlanes  = 1;
    bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;
    int nColors = (1 << bi.biBitCount);
    if( nColors > 256 )
    nColors = 0;
    dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);
    hDC1 = ::GetDC(NULL);
    hPal = SelectPalette(hDC1,hPal,FALSE);
    RealizePalette(hDC1);
    hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
    if (!hDIB)
    {
    SelectPalette(hDC1,hPal,FALSE);
    ::ReleaseDC(NULL,hDC1);
    DeleteObject(hPal);
    return ;
    }
    lpbi = (LPBITMAPINFOHEADER)hDIB;
    *lpbi = bi;
    ::GetDIBits(hDC1, hBmp, 0L, (DWORD)bi.biHeight,
    (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
    bi = *lpbi;
    if (bi.biSizeImage == 0)
    bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)* bi.biHeight;
    dwLen += bi.biSizeImage;
    if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
    hDIB = handle;
    else
    {
    GlobalFree(hDIB);
    SelectPalette(hDC1,hPal,FALSE);
    ::ReleaseDC(NULL,hDC1);
    DeleteObject(hPal);
    return ;
    }
    lpbi = (LPBITMAPINFOHEADER)hDIB;
    BOOL bGotBits = GetDIBits( hDC1, hBmp,0L,(DWORD)bi.biHeight,(LPBYTE)lpbi+ (bi.biSize + nColors * sizeof(RGBQUAD)),
    (LPBITMAPINFO)lpbi,(DWORD)DIB_RGB_COLORS);
    if( !bGotBits )
    {
    GlobalFree(hDIB);
    SelectPalette(hDC1,hPal,FALSE);
    ::ReleaseDC(NULL,hDC1);
    DeleteObject(hPal);
    return;
    }
    if(iWidth==0||iLength==0)
    {
    iWidth=lpbi->biWidth;
    iLength=lpbi->biHeight;
    iWidth=(int)(dScaleX*iWidth);
    iLength=(int)(iLength*dScaleY);
    }
    StretchDIBits(hDC,iX,iY,iWidth,iLength,0,0,lpbi->biWidth,lpbi->biHeight,(LPBYTE)lpbi  // address for bitmap bits
    + (bi.biSize + nColors * sizeof(RGBQUAD)),(LPBITMAPINFO)lpbi,DIB_RGB_COLORS,SRCCOPY);
    SelectPalette(hDC1,hPal,FALSE);
    ::ReleaseDC(NULL,hDC1);
    DeleteObject(hDIB);
    DeleteObject(hPal);
    }