BITMAP  bm;
 CBitmap  Bitmap;
 CDC   MemDC, *pDC;
 
 pDC = this->GetDC();
 
 Bitmap.LoadBitmap(IDB_BMP_IMG);
   
 MemDC.CreateCompatibleDC(pDC);
   
 MemDC.SelectObject(&Bitmap);
   
 Bitmap.GetObject(sizeof(bm), &bm);

 

pDC->BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &MemDC, 0, 0, SRCCOPY);

'MFC > Bitmap' 카테고리의 다른 글

Bitmap.h  (0) 2013.08.16
Bitmap.cpp  (0) 2013.08.16
Writing a window image to a BMP file  (0) 2013.08.08
Converting DDB to DIB  (0) 2013.08.08
memory dc to bitmap file  (0) 2013.08.08
Posted by 곰돌이짱
,
  1. BOOL WriteWindowToDIB( LPTSTR szFile, CWnd *pWnd )
  2. {
  3. CBitmap bitmap;
  4. CWindowDC dc(pWnd);
  5. CDC memDC;
  6. CRect rect;
  7.  
  8. memDC.CreateCompatibleDC(&dc);
  9.  
  10. pWnd->GetWindowRect(rect);
  11.  
  12. bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() );
  13. CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
  14. memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0, 0, SRCCOPY);
  15.  
  16. // Create logical palette if device support a palette
  17. CPalette pal;
  18. if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
  19. {
  20. UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
  21. LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
  22. pLP->palVersion = 0x300;
  23.  
  24. pLP->palNumEntries =
  25. GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry );
  26.  
  27. // Create the palette
  28. pal.CreatePalette( pLP );
  29.  
  30. delete[] pLP;
  31. }
  32.  
  33. memDC.SelectObject(pOldBitmap);
  34.  
  35. // Convert the bitmap to a DIB
  36. HANDLE hDIB = DDBToDIB( bitmap, BI_RGB, &pal );
  37.  
  38. if( hDIB == NULL )
  39. return FALSE;
  40.  
  41. // Write it to file
  42. WriteDIB( szFile, hDIB );
  43.  
  44. // Free the memory allocated by DDBToDIB for the DIB
  45. GlobalFree( hDIB );
  46. return TRUE;
  47. }

'MFC > Bitmap' 카테고리의 다른 글

Bitmap.cpp  (0) 2013.08.16
리소스에서 BMP 이미지 불러오기  (0) 2013.08.09
Converting DDB to DIB  (0) 2013.08.08
memory dc to bitmap file  (0) 2013.08.08
Bitmap Save  (0) 2013.08.08
Posted by 곰돌이짱
,

Converting DDB to DIB

MFC/Bitmap 2013. 8. 8. 17:55

A device-dependent bitmap (DDB) is structured in such a way that it matches very closely the form in which the bitmap is finally displayed by the device driver. For this reason the DDB is unlikely to be compatible with other display devices. A device-independent bitmap (DIB) on the other hand has a logical layout and can be decifered by any device driver. The disadvantage with DIB though is that it is much slower to render onto the device as compared to a DDB.

One situation where we need to convert a DDB to a DIB is when we want to save the bitmap to a file. The BMP files are infact just composed of a short header followed by information in a DIB.

The steps involved in creating the DIB are:

  1. Initialize a BITMAPINFOHEADER data structure. We use information in the bitmap to determine the widht, height and the bit count. The compression field is set based on the argument passed into the function. It's best to use the BI_RGB compression for greater portability and faster rendering.
  2. Select and realize the logical palette associated with the bitmap. This is passed in as the last argument to the function. If a palette is not provided use the default palette.
  3. Determine the number of bytes required for the bitmap bits. First allocate enough memory for BITMAPINFOHEADER and the color table and then call GetDIBits() to size. Supplying a NULL for the lpBits parameter instructs GetDIBits() to biSizeImage field in the bitmapinfoheader. Sometimes, this does not work (depending on the device driver) in which case we make our own estimate. We can compute this since we already know the width, height, the number of bits per pixel and the fact that each row of pixels will occupy a multiple of 4 bytes (32 bits).
  4. Given the final size of the bitmap bits, reallocate the memory block to hold the bitmapinfoheader, the color table and the bitmap bits.
  5. Finally call the GetDIBits() again to get the bitmap bits.
  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.  
  16.  
  17. ASSERT( bitmap.GetSafeHandle() );
  18.  
  19. // The function has no arg for bitfields
  20. if( dwCompression == BI_BITFIELDS )
  21. return NULL;
  22.  
  23. // If a palette has not been supplied use defaul palette
  24. hPal = (HPALETTE) pPal->GetSafeHandle();
  25. if (hPal==NULL)
  26. hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
  27.  
  28. // Get bitmap information
  29. bitmap.GetObject(sizeof(bm),(LPSTR)&bm);
  30.  
  31. // Initialize the bitmapinfoheader
  32. bi.biSize = sizeof(BITMAPINFOHEADER);
  33. bi.biWidth = bm.bmWidth;
  34. bi.biHeight = bm.bmHeight;
  35. bi.biPlanes = 1;
  36. bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
  37. bi.biCompression = dwCompression;
  38. bi.biSizeImage = 0;
  39. bi.biXPelsPerMeter = 0;
  40. bi.biYPelsPerMeter = 0;
  41. bi.biClrUsed = 0;
  42. bi.biClrImportant = 0;
  43.  
  44. // Compute the size of the infoheader and the color table
  45. int nColors = (1 << bi.biBitCount);
  46. if( nColors > 256 )
  47. nColors = 0;
  48. dwLen = bi.biSize + nColors * sizeof(RGBQUAD);
  49.  
  50. // We need a device context to get the DIB from
  51. hDC = GetDC(NULL);
  52. hPal = SelectPalette(hDC,hPal,FALSE);
  53. RealizePalette(hDC);
  54.  
  55. // Allocate enough memory to hold bitmapinfoheader and color table
  56. hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
  57.  
  58. if (!hDIB){
  59. SelectPalette(hDC,hPal,FALSE);
  60. ReleaseDC(NULL,hDC);
  61. return NULL;
  62. }
  63.  
  64. lpbi = (LPBITMAPINFOHEADER)hDIB;
  65.  
  66. *lpbi = bi;
  67.  
  68. // Call GetDIBits with a NULL lpBits param, so the device driver
  69. // will calculate the biSizeImage field
  70. GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
  71. (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
  72.  
  73. bi = *lpbi;
  74.  
  75. // If the driver did not fill in the biSizeImage field, then compute it
  76. // Each scan line of the image is aligned on a DWORD (32bit) boundary
  77. if (bi.biSizeImage == 0){
  78. bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
  79. * bi.biHeight;
  80.  
  81. // If a compression scheme is used the result may infact be larger
  82. // Increase the size to account for this.
  83. if (dwCompression != BI_RGB)
  84. bi.biSizeImage = (bi.biSizeImage * 3) / 2;
  85. }
  86.  
  87. // Realloc the buffer so that it can hold all the bits
  88. dwLen += bi.biSizeImage;
  89. if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
  90. hDIB = handle;
  91. else{
  92. GlobalFree(hDIB);
  93.  
  94. // Reselect the original palette
  95. SelectPalette(hDC,hPal,FALSE);
  96. ReleaseDC(NULL,hDC);
  97. return NULL;
  98. }
  99.  
  100. // Get the bitmap bits
  101. lpbi = (LPBITMAPINFOHEADER)hDIB;
  102.  
  103. // FINALLY get the DIB
  104. BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
  105. 0L, // Start scan line
  106. (DWORD)bi.biHeight, // # of scan lines
  107. (LPBYTE)lpbi // address for bitmap bits
  108. + (bi.biSize + nColors * sizeof(RGBQUAD)),
  109. (LPBITMAPINFO)lpbi, // address of bitmapinfo
  110. (DWORD)DIB_RGB_COLORS); // Use RGB for color table
  111.  
  112. if( !bGotBits )
  113. {
  114. GlobalFree(hDIB);
  115. SelectPalette(hDC,hPal,FALSE);
  116. ReleaseDC(NULL,hDC);
  117. return NULL;
  118. }
  119.  
  120. SelectPalette(hDC,hPal,FALSE);
  121. ReleaseDC(NULL,hDC);
  122. return hDIB;
  123. }

 


'MFC > Bitmap' 카테고리의 다른 글

Bitmap.cpp  (0) 2013.08.16
리소스에서 BMP 이미지 불러오기  (0) 2013.08.09
Writing a window image to a BMP file  (0) 2013.08.08
memory dc to bitmap file  (0) 2013.08.08
Bitmap Save  (0) 2013.08.08
Posted by 곰돌이짱
,