help you to up one up...

解决方案 »

  1.   

    //
    // Function : LoadBitmap()
    // Purpose  : Load the bitmap and translate to the GAPI recoginzlia memory buffer...
    //
    BOOL CCEBitmap::LoadBitmap( CCEDraw* pCEDraw, LPCTSTR lpszBitmapName )
    {
    HBITMAP hBitmap;
    BITMAP  Bitmap; // Free the buffer, if it exist...
    if( m_pBitmapBuffer ) free( m_pBitmapBuffer ); // If the lpszBitmapName == NULL, that means we create a screen bitmap...
    if( NULL == lpszBitmapName )
    {
    m_sizeBitmap.cx = pCEDraw->GetDisplayProperties().cxWidth;
    m_sizeBitmap.cy = pCEDraw->GetDisplayProperties().cyHeight;
    m_pBitmapBuffer = (unsigned char*)malloc( m_sizeBitmap.cx * m_sizeBitmap.cy * pCEDraw->GetDisplayProperties().cBPP / 8 );
    memcpy( m_pBitmapBuffer, pCEDraw->GetBuffer(), m_sizeBitmap.cx * m_sizeBitmap.cy * pCEDraw->GetDisplayProperties().cBPP / 8 );
    m_nType = CEB_TYPE_SCREENBUFFER;
    return TRUE;
    } // First to load the bitmap from file...
    hBitmap = (HBITMAP) LoadFile( lpszBitmapName );
    //hBitmap =(HBITMAP)::LoadBitmap( GetModuleHandle( NULL ), MAKEINTRESOURCE(IDB_BITMAP1));
    if( hBitmap == NULL ) 
    {
    // Failed to load the bitmap...
    return FALSE;
    }
    GetObject( hBitmap, sizeof(BITMAP), &Bitmap );

    // Creating new bitmap and receive pointer to it's bits.
    HBITMAP hTargetBitmap;
    unsigned char *pBuffer; // Initilize DIBINFO structure
    BITMAPINFO  dibInfo;
    dibInfo.bmiHeader.biBitCount = 24;
    dibInfo.bmiHeader.biClrImportant = 0;
    dibInfo.bmiHeader.biClrUsed = 0;
    dibInfo.bmiHeader.biCompression = 0;
    dibInfo.bmiHeader.biHeight = Bitmap.bmHeight;
    dibInfo.bmiHeader.biPlanes = 1;
    dibInfo.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
    dibInfo.bmiHeader.biSizeImage = Bitmap.bmWidth*Bitmap.bmHeight*3;
    dibInfo.bmiHeader.biWidth = Bitmap.bmWidth;
    dibInfo.bmiHeader.biXPelsPerMeter = 3780;
    dibInfo.bmiHeader.biYPelsPerMeter = 3780;
    dibInfo.bmiColors[0].rgbBlue = 0;
    dibInfo.bmiColors[0].rgbGreen = 0;
    dibInfo.bmiColors[0].rgbRed = 0;
    dibInfo.bmiColors[0].rgbReserved = 0; // Create bitmap and receive pointer to points into pBuffer
    HDC hDC = ::GetDC(NULL);
    ASSERT(hDC);
    hTargetBitmap = CreateDIBSection( hDC,
      (const BITMAPINFO*)&dibInfo,
      DIB_RGB_COLORS,
      (void**)&pBuffer,
      NULL,
      0
     ); ::ReleaseDC(NULL, hDC); // Copy source bitmap into the target bitmap. // Create 2 device contexts 
    HDC memDc;
    if ( !( memDc = CreateCompatibleDC(NULL) ) ) 
    {
    DeleteObject( hBitmap );
    DeleteObject( hTargetBitmap );
    return FALSE;
    } HDC targetDc;
    if ( !( targetDc = CreateCompatibleDC(NULL) ) ) 
    {
    DeleteDC( memDc );
    DeleteObject( hBitmap );
    DeleteObject( hTargetBitmap );
    return FALSE;
    } // Select source bitmap into one DC, target into another
    HBITMAP hOldBitmap1 = (HBITMAP)::SelectObject( memDc, hBitmap);
    HBITMAP hOldBitmap2 = (HBITMAP)::SelectObject( targetDc, hTargetBitmap ); // Copy source bitmap into the target one
    ::BitBlt( targetDc, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, memDc, 0, 0, SRCCOPY ); // Create my own bitmap buffer use the GAPI recognized style...
    // Free the memory if it exist... int nX, nY;
    unsigned short nColor;
    int nBitmapYPitch = ((Bitmap.bmWidth*3)+3) & 0xfffc; BOOL   bReturn = TRUE;
    if( !m_pBitmapBuffer ) free( m_pBitmapBuffer ); long cByte = pCEDraw->GetDisplayProperties().cBPP / 8;
    m_sizeBitmap.cx = Bitmap.bmWidth;
    m_sizeBitmap.cy = Bitmap.bmHeight;
    m_pBitmapBuffer = (unsigned char*)malloc( Bitmap.bmWidth * Bitmap.bmHeight * cByte );
    if( !m_pBitmapBuffer ) 
    {
    bReturn = FALSE;
    goto Exit;
    }    /* Warning : Does not support 8 byte color model in here */
    for( nY = 0; nY < Bitmap.bmHeight ; nY ++ )
    {
    for( nX = 0; nX < Bitmap.bmWidth ; nX ++ )
    { nColor = pCEDraw->GetBitmapPointColor( ( pBuffer +  ( nX ) * 3 +
                   ( Bitmap.bmHeight - nY - 1 ) * 
                     nBitmapYPitch ) );
    if( cByte == 2 )
    *(unsigned short*)( m_pBitmapBuffer + ( nX + nY * Bitmap.bmWidth ) * cByte ) = nColor;
    else *( m_pBitmapBuffer + ( nX + nY * Bitmap.bmWidth ) * cByte ) = (BYTE)nColor;

    }
    } // Restore device contexts
    Exit:
    ::SelectObject( memDc, hOldBitmap1 );
    ::SelectObject( targetDc, hOldBitmap2 );
    DeleteDC( memDc );
    DeleteDC( targetDc );
    DeleteObject( hBitmap );
    DeleteObject( hTargetBitmap );
    return bReturn;
    }
      

  2.   

    Sorry,代码比较乱,看不懂得化问我,主要是用CreateDIBSection,然后用2个device contex
      

  3.   

    我觉得这样程序的运行效率不太高吧,有直接得到HBITMAP数据在内存的地址的方法吗?
      

  4.   

    这可是微软工程师告诉我的方法了,应为hbitmap在windows中算是系统的部分,我当时就是想查hbitmap是否是一结构,但是查来查去都没查出来,只有问微软的人,结果就是我写的上面的代码了:)