用GetDibits函数将数据考出有点慢,我想直接对原内存中数据操作,但不知如何得到数据地址,最好不用mfc.

解决方案 »

  1.   

    只能够用GetDibits没有别的方法
      

  2.   

    看下面的代码:BITMAPINFO * BitmapToDIB(HPALETTE hPal, // palette for color conversion
     HBITMAP  hBmp, // DDB for convert
     int nBitCount, int nCompression) // format wanted
    {
    typedef struct
    {
    BITMAPINFOHEADER bmiHeader;
    RGBQUAD        bmiColors[256+3];
    } DIBINFO; BITMAP  ddbinfo;
    DIBINFO dibinfo; // retrieve DDB information
    if ( GetObject(hBmp, sizeof(BITMAP), & ddbinfo)==0 )
    return NULL; // fill out BITMAPINFOHEADER based on size and required format
    memset(&dibinfo, 0, sizeof(dibinfo)); dibinfo.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);
    dibinfo.bmiHeader.biWidth       = ddbinfo.bmWidth;
    dibinfo.bmiHeader.biHeight      = ddbinfo.bmHeight;
    dibinfo.bmiHeader.biPlanes      = 1;
    dibinfo.bmiHeader.biBitCount    = nBitCount;
    dibinfo.bmiHeader.biCompression = nCompression; HDC     hDC = GetDC(NULL); // screen DC
    HGDIOBJ hpalOld;

    if ( hPal )
    hpalOld = SelectPalette(hDC, hPal, FALSE);
    else
    hpalOld = NULL; // query GDI for image size
    GetDIBits(hDC, hBmp, 0, ddbinfo.bmHeight, NULL, (BITMAPINFO *) & dibinfo, DIB_RGB_COLORS); int nInfoSize  = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * GetDIBColorCount(dibinfo.bmiHeader);
    int nTotalSize = nInfoSize + GetDIBPixelSize(dibinfo.bmiHeader); BYTE * pDIB = new BYTE[nTotalSize]; if ( pDIB )
    {
    memcpy(pDIB, & dibinfo, nInfoSize);

    if ( ddbinfo.bmHeight != GetDIBits(hDC, hBmp, 0, ddbinfo.bmHeight, pDIB + nInfoSize, (BITMAPINFO *) pDIB, DIB_RGB_COLORS) )
    {
    delete [] pDIB;
    pDIB = NULL;
    }
    } if ( hpalOld )
    SelectObject(hDC, hpalOld); ReleaseDC(NULL, hDC); return (BITMAPINFO *) pDIB;
    }
      

  3.   

    还有几个函数
    int GetDIBPixelSize(const BITMAPINFOHEADER & bmih)
    {
    if ( bmih.biSizeImage )
    return bmih.biSizeImage;
    else
    return ( bmih.biWidth * bmih.biBitCount + 31 ) / 32 * 4 * bmih.biPlanes * abs(bmih.biHeight);
    }int GetDIBColorCount(const BITMAPINFOHEADER & bmih)
    {
    if ( bmih.biBitCount <= 8 )
    if ( bmih.biClrUsed )
    return bmih.biClrUsed;
    else
    return 1 << bmih.biBitCount;
    else if ( bmih.biCompression==BI_BITFIELDS )
    return 3 + bmih.biClrUsed;
    else
    return bmih.biClrUsed;
    }