CWindowToBMP Class Reference

A class for writing a windows content to a BMP file. More...

#include <WindowToBMP.h>

List of all members.

Public Member Functions

bool Write (CString file, CWnd *oWnd)
 CWindowToBMP ()
virtual ~CWindowToBMP ()

Private Member Functions

BOOL WriteWindowToDIB (LPTSTR szFile, CWnd *pWnd)
HANDLE DDBToDIB (CBitmap &bitmap, DWORD dwCompression, CPalette *pPal)
BOOL WriteDIB (LPTSTR szFile, HANDLE hDIB)


Detailed Description

A class for writing a windows content to a BMP file.

Definition at line 14 of file WindowToBMP.h.


Constructor & Destructor Documentation

CWindowToBMP::CWindowToBMP (  ) 

Definition at line 19 of file WindowToBMP.cpp.

00020 {
00021 
00022 }

CWindowToBMP::~CWindowToBMP (  )  [virtual]

Definition at line 24 of file WindowToBMP.cpp.

00025 {
00026 
00027 }


Member Function Documentation

bool CWindowToBMP::Write ( CString  file,
CWnd *  oWnd 
)

Definition at line 29 of file WindowToBMP.cpp.

References WriteWindowToDIB().

Referenced by CEvolveTrafficView::OnFileSaveImage().

00030 {
00031           char* ch = file.GetBuffer(0);
00032           return WriteWindowToDIB( ch, pWnd );
00033 }

Here is the call graph for this function:

BOOL CWindowToBMP::WriteWindowToDIB ( LPTSTR  szFile,
CWnd *  pWnd 
) [private]

Definition at line 35 of file WindowToBMP.cpp.

References DDBToDIB(), and WriteDIB().

Referenced by Write().

00036 {
00037         CBitmap         bitmap;
00038         CWindowDC       dc(pWnd);
00039         CDC             memDC;
00040         CRect           rect;
00041 
00042         memDC.CreateCompatibleDC(&dc);
00043 
00044         pWnd->GetWindowRect(rect);
00045 
00046         bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() );
00047 
00048         CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
00049         memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0, 0, SRCCOPY);
00050 
00051         // Create logical palette if device support a palette
00052         CPalette pal;
00053         if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
00054         {
00055                 UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
00056                 LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
00057                 pLP->palVersion = 0x300;
00058 
00059                 pLP->palNumEntries =
00060                         GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry );
00061 
00062                 // Create the palette
00063                 pal.CreatePalette( pLP );
00064 
00065                 delete[] pLP;
00066         }
00067 
00068         memDC.SelectObject(pOldBitmap);
00069 
00070         // Convert the bitmap to a DIB
00071         HANDLE hDIB = DDBToDIB( bitmap, BI_RGB, &pal );
00072 
00073         if( hDIB == NULL )
00074                 return FALSE;
00075 
00076         // Write it to file
00077         WriteDIB( szFile, hDIB );
00078 
00079         // Free the memory allocated by DDBToDIB for the DIB
00080         GlobalFree( hDIB );
00081         return TRUE;
00082 }

Here is the call graph for this function:

HANDLE CWindowToBMP::DDBToDIB ( CBitmap &  bitmap,
DWORD  dwCompression,
CPalette *  pPal 
) [private]

Definition at line 112 of file WindowToBMP.cpp.

Referenced by WriteWindowToDIB().

00113 {
00114         BITMAP                  bm;
00115         BITMAPINFOHEADER        bi;
00116         LPBITMAPINFOHEADER      lpbi;
00117         DWORD                   dwLen;
00118         HANDLE                  hDIB;
00119         HANDLE                  handle;
00120         HDC                     hDC;
00121         HPALETTE                hPal;
00122 
00123 
00124         ASSERT( bitmap.GetSafeHandle() );
00125 
00126         // The function has no arg for bitfields
00127         if( dwCompression == BI_BITFIELDS )
00128                 return NULL;
00129 
00130         // If a palette has not been supplied use defaul palette
00131         hPal = (HPALETTE) pPal->GetSafeHandle();
00132         if (hPal==NULL)
00133                 hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
00134 
00135         // Get bitmap information
00136         bitmap.GetObject(sizeof(bm),(LPSTR)&bm);
00137 
00138         // Initialize the bitmapinfoheader
00139         bi.biSize               = sizeof(BITMAPINFOHEADER);
00140         bi.biWidth              = bm.bmWidth;
00141         bi.biHeight             = bm.bmHeight;
00142         bi.biPlanes             = 1;
00143         bi.biBitCount           = bm.bmPlanes * bm.bmBitsPixel;
00144         bi.biCompression        = dwCompression;
00145         bi.biSizeImage          = 0;
00146         bi.biXPelsPerMeter      = 0;
00147         bi.biYPelsPerMeter      = 0;
00148         bi.biClrUsed            = 0;
00149         bi.biClrImportant       = 0;
00150 
00151         // Compute the size of the  infoheader and the color table
00152         int nColors = (1 << bi.biBitCount);
00153         if( nColors > 256 )
00154                 nColors = 0;
00155         dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);
00156 
00157         // We need a device context to get the DIB from
00158         hDC = GetDC(NULL);
00159         hPal = SelectPalette(hDC,hPal,FALSE);
00160         RealizePalette(hDC);
00161 
00162         // Allocate enough memory to hold bitmapinfoheader and color table
00163         hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
00164 
00165         if (!hDIB){
00166                 SelectPalette(hDC,hPal,FALSE);
00167                 ReleaseDC(NULL,hDC);
00168                 return NULL;
00169         }
00170 
00171         lpbi = (LPBITMAPINFOHEADER)hDIB;
00172 
00173         *lpbi = bi;
00174 
00175         // Call GetDIBits with a NULL lpBits param, so the device driver 
00176         // will calculate the biSizeImage field 
00177         GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
00178                         (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
00179 
00180         bi = *lpbi;
00181 
00182         // If the driver did not fill in the biSizeImage field, then compute it
00183         // Each scan line of the image is aligned on a DWORD (32bit) boundary
00184         if (bi.biSizeImage == 0){
00185                 bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
00186                                                 * bi.biHeight;
00187 
00188                 // If a compression scheme is used the result may infact be larger
00189                 // Increase the size to account for this.
00190                 if (dwCompression != BI_RGB)
00191                         bi.biSizeImage = (bi.biSizeImage * 3) / 2;
00192         }
00193 
00194         // Realloc the buffer so that it can hold all the bits
00195         dwLen += bi.biSizeImage;
00196         if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
00197                 hDIB = handle;
00198         else{
00199                 GlobalFree(hDIB);
00200 
00201                 // Reselect the original palette
00202                 SelectPalette(hDC,hPal,FALSE);
00203                 ReleaseDC(NULL,hDC);
00204                 return NULL;
00205         }
00206 
00207         // Get the bitmap bits
00208         lpbi = (LPBITMAPINFOHEADER)hDIB;
00209 
00210         // FINALLY get the DIB
00211         BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
00212                                 0L,                             // Start scan line
00213                                 (DWORD)bi.biHeight,             // # of scan lines
00214                                 (LPBYTE)lpbi                    // address for bitmap bits
00215                                 + (bi.biSize + nColors * sizeof(RGBQUAD)),
00216                                 (LPBITMAPINFO)lpbi,             // address of bitmapinfo
00217                                 (DWORD)DIB_RGB_COLORS);         // Use RGB for color table
00218 
00219         if( !bGotBits )
00220         {
00221                 GlobalFree(hDIB);
00222 
00223                 SelectPalette(hDC,hPal,FALSE);
00224                 ReleaseDC(NULL,hDC);
00225                 return NULL;
00226         }
00227 
00228         SelectPalette(hDC,hPal,FALSE);
00229         ReleaseDC(NULL,hDC);
00230         return hDIB;
00231 }

BOOL CWindowToBMP::WriteDIB ( LPTSTR  szFile,
HANDLE  hDIB 
) [private]

Definition at line 84 of file WindowToBMP.cpp.

Referenced by WriteWindowToDIB().

00085 {
00086         BITMAPFILEHEADER hdr;
00087         LPBITMAPINFOHEADER lpbi;
00088 
00089         if (!hDIB)
00090         return FALSE;
00091 
00092         CFile file;
00093         if (!file.Open (szFile, CFile::modeWrite | CFile::modeCreate))
00094         return FALSE;
00095 
00096         lpbi = (LPBITMAPINFOHEADER)hDIB;
00097         int nColors = lpbi->biBitCount;
00098 
00099         hdr.bfType = ((WORD) ('M' << 8) | 'B');
00100         hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
00101         hdr.bfReserved1 = 0;
00102         hdr.bfReserved2 = 0;
00103         hdr.bfOffBits = (DWORD) (sizeof( hdr ) + sizeof(BITMAPINFOHEADER));
00104 
00105         file.Write( &hdr, sizeof(hdr) );
00106         file.Write( lpbi, GlobalSize(hDIB) );
00107         file.Close ();
00108 
00109         return TRUE;
00110 }


The documentation for this class was generated from the following files:

Generated on Wed Aug 20 00:48:47 2008 for EvolveTraffic by  doxygen 1.5.6