Create a bitmap file
C / C++ / MFC
1
Posts
1
Posters
0
Views
1
Watching
-
I'm trying to create a bitmap file from a memory device context but the only thing I create is a complete white bitmap. X| Here my code:
MyFunction()
{
CClientDC dc(this); //dc of dialog
CRect rectBF(0,0,450,450);
CBitmap bmp, *poldbmp;
CDC memdc;// Load the bitmap resource bmp.CreateCompatibleBitmap(&dc, 450, 450); // Create a compatible memory DC memdc.CreateCompatibleDC( &dc ); // Select the bitmap into the DC poldbmp = memdc.SelectObject( &bmp ); //CGraphWnd was created by Paul Barvinko - [A 2D data visualisation class](http://www.codeproject.com/miscctrl/graph2d.asp) m\_ctlGraphWnd.DrawGraphToDC(&memdc, rectBF); //Copy graph to DC memdc.SelectObject(poldbmp); CPalette palette; //Initialize palette if( dc.GetDeviceCaps(RASTERCAPS) & RC\_PALETTE ) { UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) \* 256); LOGPALETTE \*pLP = (LOGPALETTE \*) new BYTE\[nSize\]; pLP->palVersion = 0x300; pLP->palNumEntries = GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry ); // Create the palette palette.CreatePalette( pLP ); delete\[\] pLP; }
This code above works correctly. The graph was drawn in memdc.
//Function was created by Zafir Anjum - [Converting DDB to DIB](http://www.codeguru.com/bitmap/ddb_to_dib.shtml) HANDLE hDIB = DDBToDIB(bmp, BI\_RGB, &palette); if( hDIB == NULL ) return ; //Function was created by Zafir Anjum - [Writing a bitmap to a BMP file](http://www.codeguru.com/bitmap/bitmap_to_file.shtml) WriteDIB(\_T("graph.bmp"), hDIB); GlobalFree( hDIB );
}
I don't know why but operations done on memdc aren't reflected in CBitmap bmp. Thanks in advance!