DDB和DIB相互转化

DDB:存在于内存中的

DIB:存在于硬盘中的(.bmp)

  1. // DDBToDIB     - Creates a DIB from a DDB
  2. // bitmap       - Device dependent bitmap
  3. // dwCompression    - Type of compression - see BITMAPINFOHEADER
  4. // pPal         - Logical palette
  5. HANDLE DDBToDIB( CBitmap& bitmap, DWORD dwCompression, CPalette* pPal ) 
  6. {
  7.     BITMAP          bm;
  8.     BITMAPINFOHEADER    bi;
  9.     LPBITMAPINFOHEADER  lpbi;
  10.     DWORD           dwLen;
  11.     HANDLE          hDIB;
  12.     HANDLE          handle;
  13.     HDC             hDC;
  14.     HPALETTE        hPal;
  15.     ASSERT( bitmap.GetSafeHandle() );
  16.     // The function has no arg for bitfields
  17.     if( dwCompression == BI_BITFIELDS )
  18.         return NULL;
  19.     // If a palette has not been supplied use defaul palette
  20.     hPal = (HPALETTE) pPal->GetSafeHandle();
  21.     if (hPal==NULL)
  22.         hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
  23.     // Get bitmap information
  24.     bitmap.GetObject(sizeof(bm),(LPSTR)&bm);
  25.     // Initialize the bitmapinfoheader
  26.     bi.biSize       = sizeof(BITMAPINFOHEADER);
  27.     bi.biWidth      = bm.bmWidth;
  28.     bi.biHeight         = bm.bmHeight;
  29.     bi.biPlanes         = 1;
  30.     bi.biBitCount       = bm.bmPlanes * bm.bmBitsPixel;
  31.     bi.biCompression    = dwCompression;
  32.     bi.biSizeImage      = 0;
  33.     bi.biXPelsPerMeter  = 0;
  34.     bi.biYPelsPerMeter  = 0;
  35.     bi.biClrUsed        = 0;
  36.     bi.biClrImportant   = 0;
  37.     // Compute the size of the  infoheader and the color table
  38.     int nColors = (1 << bi.biBitCount);
  39.     if( nColors > 256 ) 
  40.         nColors = 0;
  41.     dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);
  42.     // We need a device context to get the DIB from
  43.     hDC = GetDC(NULL);
  44.     hPal = SelectPalette(hDC,hPal,FALSE);
  45.     RealizePalette(hDC);
  46.     // Allocate enough memory to hold bitmapinfoheader and color table
  47.     hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
  48.     if (!hDIB){
  49.         SelectPalette(hDC,hPal,FALSE);
  50.         ReleaseDC(NULL,hDC);
  51.         return NULL;
  52.     }
  53.     lpbi = (LPBITMAPINFOHEADER)hDIB;
  54.     *lpbi = bi;
  55.     // Call GetDIBits with a NULL lpBits param, so the device driver 
  56.     // will calculate the biSizeImage field 
  57.     GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
  58.             (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
  59.     bi = *lpbi;
  60.     // If the driver did not fill in the biSizeImage field, then compute it
  61.     // Each scan line of the image is aligned on a DWORD (32bit) boundary
  62.     if (bi.biSizeImage == 0){
  63.         bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) 
  64.                         * bi.biHeight;
  65.         // If a compression scheme is used the result may infact be larger
  66.         // Increase the size to account for this.
  67.         if (dwCompression != BI_RGB)
  68.             bi.biSizeImage = (bi.biSizeImage * 3) / 2;
  69.     }
  70.     // Realloc the buffer so that it can hold all the bits
  71.     dwLen += bi.biSizeImage;
  72.     if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
  73.         hDIB = handle;
  74.     else{
  75.         GlobalFree(hDIB);
  76.         // Reselect the original palette
  77.         SelectPalette(hDC,hPal,FALSE);
  78.         ReleaseDC(NULL,hDC);
  79.         return NULL;
  80.     }
  81.     // Get the bitmap bits
  82.     lpbi = (LPBITMAPINFOHEADER)hDIB;
  83.     // FINALLY get the DIB
  84.     BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
  85.                 0L,             // Start scan line
  86.                 (DWORD)bi.biHeight,     // # of scan lines
  87.                 (LPBYTE)lpbi            // address for bitmap bits
  88.                 + (bi.biSize + nColors * sizeof(RGBQUAD)),
  89.                 (LPBITMAPINFO)lpbi,     // address of bitmapinfo
  90.                 (DWORD)DIB_RGB_COLORS);     // Use RGB for color table
  91.     if( !bGotBits )
  92.     {
  93.         GlobalFree(hDIB);
  94.         
  95.         SelectPalette(hDC,hPal,FALSE);
  96.         ReleaseDC(NULL,hDC);
  97.         return NULL;
  98.     }
  99.     SelectPalette(hDC,hPal,FALSE);
  100.     ReleaseDC(NULL,hDC);
  101.     return hDIB;
  102. }

 

  1. HBITMAP DIBToDDB( HANDLE hDIB ) 
  2. LPBITMAPINFOHEADER lpbi; 
  3. HBITMAP hbm; 
  4. CPalette pal; 
  5. CPalette* pOldPal; 
  6. CClientDC dc(NULL); 
  7. if (hDIB == NULL) 
  8. return NULL; 
  9. lpbi = (LPBITMAPINFOHEADER)hDIB; 
  10. int nColors = lpbi->biClrUsed ? lpbi->biClrUsed : 
  11. 1 << lpbi->biBitCount; 
  12. BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ; 
  13. LPVOID lpDIBBits; 
  14. if( bmInfo.bmiHeader.biBitCount > 8 ) 
  15. lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors + 
  16. bmInfo.bmiHeader.biClrUsed) + 
  17. ((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0)); 
  18. else 
  19. lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors); 
  20. // Create and select a logical palette if needed 
  21. if( nColors <= 256 && dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE) 
  22. UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors); 
  23. LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize]; 
  24. pLP->palVersion = 0x300; 
  25. pLP->palNumEntries = nColors; 
  26. forint i=0; i < nColors; i++) 
  27. pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed; 
  28. pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen; 
  29. pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue; 
  30. pLP->palPalEntry[i].peFlags = 0; 
  31. pal.CreatePalette( pLP ); 
  32. delete[] pLP; 
  33. // Select and realize the palette 
  34. pOldPal = dc.SelectPalette( &pal, FALSE ); 
  35. dc.RealizePalette(); 
  36. hbm = CreateDIBitmap(dc.GetSafeHdc(), // handle to device context 
  37. (LPBITMAPINFOHEADER)lpbi, // pointer to bitmap info header 
  38. (LONG)CBM_INIT, // initialization flag 
  39. lpDIBBits, // pointer to initialization data 
  40. (LPBITMAPINFO)lpbi, // pointer to bitmap info 
  41. DIB_RGB_COLORS ); // color-data usage 
  42. if (pal.GetSafeHandle()) 
  43. dc.SelectPalette(pOldPal,FALSE); 
  44. return hbm; 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值