这段代码作用是把hDIB转成GDI API可以直接操作的BITMAP对象
有个问题 我传入的hDIB明明是256色 也就是8 bpp的 为什么
它反给我的BITMAP会变成我当前桌面的色深?我桌面32它也32
16 它也16 !! 为什么不是我要的256色 or 8 bpp?? 
各位大虾看能不能修改一下这代码 把问题给纠正了
谢谢
HBITMAP BitmapFromDIB (HANDLE hDIB, HPALETTE  hPal)
{
    LPBITMAPINFOHEADER  lpbih;
    HPALETTE            hPalOld;
    HDC                 hDC;
    HBITMAP             hBitmap;  
    if (!hDIB)
        return NULL;    lpbih = (LPBITMAPINFOHEADER)GlobalLock(hDIB);    if (!lpbih)
        return NULL;    hDC = GetDC(NULL);    if (hPal){
        hPalOld = SelectPalette(hDC, hPal, FALSE);
        RealizePalette(hDC); 
    }                              
   
    hBitmap = CreateDIBitmap(hDC, 
                lpbih, 
                CBM_INIT, 
                (LPSTR)lpbih + lpbih->biSize + ColorTableSize(lpbih), 
                (LPBITMAPINFO)lpbih, 
                DIB_RGB_COLORS );     if (hPal)
        SelectPalette(hDC, hPalOld, FALSE);    ReleaseDC(NULL, hDC);
    GlobalUnlock(hDIB);
    return hBitmap;
}

解决方案 »

  1.   

    hDC = GetDC(NULL)
    是取系统默认的HDC句柄 !
      

  2.   

    那怎么办? 不用这个hDC那用什么?
      

  3.   

    hDC = GetDC(NULL)并没有错,而是在hPal装入hDC前要将调色板转换成DIB所接受的,如下:
    if (!hPal)
       {
          CPalette    DibPal; 
          CreateDIBPalette(hDib,&DibPal);
          hPal = (HPALETTE) DibPal.Detach();
       }
       hOldPal = ::SelectPalette(hDC, hPal, FALSE);
       ::RealizePalette(hDC);其中CreateDIBPalette()函数如下:
    BOOL CreateDIBPalette(HDIB hDIB, CPalette* pPal)
    {
       LPLOGPALETTE lpPal;      // pointer to a logical palette
       HANDLE hLogPal;          // handle to a logical palette
       HPALETTE hPal = NULL;    // handle to a palette
       int i;                   // loop index
       WORD wNumColors;         // number of colors in color table
       LPSTR lpbi;              // pointer to packed-DIB
       LPBITMAPINFO lpbmi;      // pointer to BITMAPINFO structure (Win3.0)
       LPBITMAPCOREINFO lpbmc;  // pointer to BITMAPCOREINFO structure (old)
       BOOL bWinStyleDIB;       // flag which signifies whether this is a Win3.0 DIB
       BOOL bResult = FALSE;
       
       /* if handle to DIB is invalid, return FALSE */
       
       if (hDIB == NULL)
          return FALSE;
       
       lpbi = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
       
       /* get pointer to BITMAPINFO (Win 3.0) */
       lpbmi = (LPBITMAPINFO)lpbi;
       
       /* get pointer to BITMAPCOREINFO (old 1.x) */
       lpbmc = (LPBITMAPCOREINFO)lpbi;
       
       /* get the number of colors in the DIB */
       wNumColors = ::DIBNumColors(lpbi);
       
       if (wNumColors != 0)
       {
          /* allocate memory block for logical palette */
          hLogPal = ::GlobalAlloc(GHND, sizeof(LOGPALETTE)
             + sizeof(PALETTEENTRY)
             * wNumColors);
          
          /* if not enough memory, clean up and return NULL */
          if (hLogPal == 0)
          {
             ::GlobalUnlock((HGLOBAL) hDIB);
             return FALSE;
          }
          
          lpPal = (LPLOGPALETTE) ::GlobalLock((HGLOBAL) hLogPal);
          
          /* set version and number of palette entries */
          lpPal->palVersion = PALVERSION;
          lpPal->palNumEntries = (WORD)wNumColors;
          
          /* is this a Win 3.0 DIB? */
          bWinStyleDIB = IS_WIN30_DIB(lpbi);
          for (i = 0; i < (int)wNumColors; i++)
          {
             if (bWinStyleDIB)
             {
                lpPal->palPalEntry[i].peRed = lpbmi->bmiColors[i].rgbRed;
                lpPal->palPalEntry[i].peGreen = lpbmi->bmiColors[i].rgbGreen;
                lpPal->palPalEntry[i].peBlue = lpbmi->bmiColors[i].rgbBlue;
                lpPal->palPalEntry[i].peFlags = 0;
             }
             else
             {
                lpPal->palPalEntry[i].peRed = lpbmc->bmciColors[i].rgbtRed;
                lpPal->palPalEntry[i].peGreen = lpbmc->bmciColors[i].rgbtGreen;
                lpPal->palPalEntry[i].peBlue = lpbmc->bmciColors[i].rgbtBlue;
                lpPal->palPalEntry[i].peFlags = 0;
             }
          }
          
          /* create the palette and get handle to it */
          bResult = pPal->CreatePalette(lpPal);
          ::GlobalUnlock((HGLOBAL) hLogPal);
          ::GlobalFree((HGLOBAL) hLogPal);
       }
       else
       {
          CWindowDC dcScreen(NULL);
          
          if ( dcScreen.GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
          {
             /* create the palette and get handle to it */
             bResult = pPal->CreateHalftonePalette(&dcScreen);
          }
          else
          {
             pPal->DeleteObject();
             bResult = TRUE;
          }
       }
       
       ::GlobalUnlock((HGLOBAL) hDIB);
       
       return bResult;
    }
      

  4.   


    HBITMAP CDib::CreateSection(CDC* pDC /* = NULL */)
    {
    if(m_lpBMIH == NULL) return NULL;
    if(m_lpImage != NULL) return NULL; // can only do this if image doesn't exist
    m_hBitmap = ::CreateDIBSection(pDC->GetSafeHdc(), (LPBITMAPINFO) m_lpBMIH,
    DIB_RGB_COLORS, (LPVOID*) &m_lpImage, NULL, 0);
    ASSERT(m_lpImage != NULL);
    return m_hBitmap;
    }
      

  5.   

    if (hPal){
            hPalOld = SelectPalette(hDC, hPal, FALSE);
            RealizePalette(hDC); 
        }                              我的代码里已经有这个了啊 hPal就是用CreateDIBPalette 建立的