Click here to Skip to main content
15,912,205 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How can relese memory from controls? Pin
Mark Salsbery28-Nov-08 12:11
Mark Salsbery28-Nov-08 12:11 
QuestionSave CBitmap to File Pin
Dhiraj kumar Saini27-Nov-08 0:44
Dhiraj kumar Saini27-Nov-08 0:44 
AnswerRe: Save CBitmap to File Pin
Emilio Garavaglia27-Nov-08 1:37
Emilio Garavaglia27-Nov-08 1:37 
AnswerRe: Save CBitmap to File Pin
CPallini27-Nov-08 1:49
mveCPallini27-Nov-08 1:49 
GeneralRe: Save CBitmap to File Pin
Dhiraj kumar Saini27-Nov-08 20:48
Dhiraj kumar Saini27-Nov-08 20:48 
GeneralRe: Save CBitmap to File Pin
CPallini27-Nov-08 22:04
mveCPallini27-Nov-08 22:04 
GeneralRe: Save CBitmap to File Pin
Hamid_RT27-Nov-08 23:38
Hamid_RT27-Nov-08 23:38 
GeneralRe: Save CBitmap to File Pin
Dhiraj kumar Saini27-Nov-08 23:53
Dhiraj kumar Saini27-Nov-08 23:53 
Ya sure.
void CCaptureDesktopDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	
CString sFName;
CDC ScreenDC;
ScreenDC.Attach(::GetDC(NULL));

CBitmap Capture;
CSize   Dimensions(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
Capture.CreateCompatibleBitmap(&ScreenDC, Dimensions.cx, Dimensions.cy);

CDC MemDC;
MemDC.CreateCompatibleDC(&ScreenDC);


CBitmap *OldBitmap = MemDC.SelectObject(&Capture);

MemDC.BitBlt(0, 0, Dimensions.cx, Dimensions.cy, &ScreenDC, 0, 0, SRCCOPY);

//added on 28Nov08

CFileDialog fd( FALSE,        // true for open, false for save  
  
_T("BMP"),       // default extention  
  
NULL,        // initial filename in box);  
  
OFN_HIDEREADONLY & // flags for detailed behaviour  
  
OFN_OVERWRITEPROMPT,  
  
NULL,        // file-filter pairs  
  
NULL);       // pointer to parent window  
  
fd.m_ofn.lpstrTitle    = _T("Enter file name...");  
  
fd.m_ofn.lpstrInitialDir = NULL;  
  
fd.m_ofn.lpstrFilter   = _T("BMP files (*.bmp)\000*.BMP\000");  
  
if(fd.DoModal() == IDOK)  
  
sFName = fd.GetPathName();  
  
else  
  
return;  
  
  
HANDLE hDib = DDBToDIB( Capture, BI_RGB, NULL );  
  
BOOL b = WriteDIB( sFName.GetBuffer(sFName.GetLength()) , hDib);  
  
if(!b)  
  
{  
  
CString sMess = _T("Unable to write to file: \n");  
  
sMess += sFName;  
  
AfxMessageBox(sMess);  
  
}  
  
  

MemDC.SelectObject(Capture);  
  
Capture.Detach();  // make sure bitmap not deleted with CBitmap object  



//----------------

MemDC.SelectObject(OldBitmap);
MemDC.DeleteDC();

::ReleaseDC(NULL, ScreenDC.Detach());


}
BOOL CCaptureDesktopDlg::WriteDIB( LPTSTR szFile, HANDLE hDIB)  
  
{  
  
BITMAPFILEHEADER hdr;  
  
LPBITMAPINFOHEADER lpbi;  
  
if (!hDIB)  
  
return FALSE;  
  
CFile file;  
  
if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )  
  
return FALSE;  
  
lpbi = (LPBITMAPINFOHEADER)hDIB;  
  
/* int nColors = 1 << lpbi->biBitCount;  
  
*/  
  
int nColors = 1 << lpbi->biBitCount;  
  
if (nColors > 256)  
  
nColors = 0;  
  
// Fill in the fields of the file header  
  
hdr.bfType  = ((WORD) ('M' << 8) | 'B'); // is always "BM"  
  
hdr.bfSize  = GlobalSize (hDIB) + sizeof( hdr );  
  
hdr.bfReserved1  = 0;  
  
hdr.bfReserved2  = 0;  
  
//// hdr.bfOffBits  = (DWORD) (sizeof( hdr ) + lpbi->biSize +  
  
////      nColors * sizeof(RGBQUAD));  
  
hdr.bfOffBits  = (lpbi->biBitCount>8)?(DWORD) (sizeof(hdr) +  
  
lpbi->biSize):(DWORD) (sizeof( hdr ) +  
  
lpbi->biSize + nColors * sizeof(RGBQUAD));  
  
// Write the file header  
  
file.Write( &hdr, sizeof(hdr) );  
  
// Write the DIB header and the bits  
  
file.Write( lpbi, GlobalSize(hDIB) );  
  
return TRUE;  
  
}  


HANDLE CCaptureDesktopDlg::DDBToDIB( CBitmap& bitmap, DWORD dwCompression, CPalette* pPal )  
  
{  
  
BITMAP   bm;  
  
BITMAPINFOHEADER bi;  
  
LPBITMAPINFOHEADER  lpbi;  
  
DWORD   dwLen;  
  
HANDLE   hDIB;  
  
HANDLE   handle;  
  
HDC    hDC;  
  
HPALETTE  hPal;  
  
ASSERT( bitmap.GetSafeHandle() );  
  
// The function has no arg for bitfields  
  
if( dwCompression == BI_BITFIELDS )  
  
return NULL;  
  
// If a palette has not been supplied use defaul palette  
  
hPal = (HPALETTE) pPal->GetSafeHandle();  
  
if (hPal==NULL)  
  
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);  
  
// Get bitmap information  
  
bitmap.GetObject(sizeof(bm),(LPSTR)&bm);  
  
// Initialize the bitmapinfoheader  
  
bi.biSize  = sizeof(BITMAPINFOHEADER);  
  
bi.biWidth  = bm.bmWidth;  
  
bi.biHeight   = bm.bmHeight;  
  
bi.biPlanes   = 1;  
  
bi.biBitCount  = (short)(bm.bmPlanes * bm.bmBitsPixel);  
  
bi.biCompression = dwCompression;  
  
bi.biSizeImage  = 0;  
  
bi.biXPelsPerMeter = 0;  
  
bi.biYPelsPerMeter = 0;  
  
bi.biClrUsed  = 0;  
  
bi.biClrImportant = 0;  
  
// Compute the size of the  infoheader and the color table  
  
int nColors = (1 << bi.biBitCount);  
  
if( nColors > 256 )  
  
nColors = 0;  
  
dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);  
  
// We need a device context to get the DIB from  
  
hDC = ::GetDC(NULL);  
  
hPal = SelectPalette(hDC,hPal,FALSE);  
  
RealizePalette(hDC);  
  
// Allocate enough memory to hold bitmapinfoheader and color table  
  
hDIB = GlobalAlloc(GMEM_FIXED,dwLen);  
  
if (!hDIB){  
  
SelectPalette(hDC,hPal,FALSE);  
  
::ReleaseDC(NULL,hDC);  
  
return NULL;  
  
}  

GeneralRe: Save CBitmap to File Pin
Hamid_RT28-Nov-08 1:49
Hamid_RT28-Nov-08 1:49 
GeneralRe: Save CBitmap to File Pin
Dhiraj kumar Saini28-Nov-08 2:33
Dhiraj kumar Saini28-Nov-08 2:33 
GeneralRe: Save CBitmap to File Pin
Hamid_RT28-Nov-08 2:34
Hamid_RT28-Nov-08 2:34 
QuestionSQL 2005 data insertion issue Pin
Riaan77727-Nov-08 0:39
Riaan77727-Nov-08 0:39 
QuestionProblem in using system() Pin
Nandu_77b27-Nov-08 0:35
Nandu_77b27-Nov-08 0:35 
AnswerRe: Problem in using system() Pin
Code-o-mat27-Nov-08 0:42
Code-o-mat27-Nov-08 0:42 
QuestionRe: Problem in using system() Pin
Nandu_77b27-Nov-08 1:19
Nandu_77b27-Nov-08 1:19 
GeneralRe: Problem in using system() Pin
sashoalm27-Nov-08 2:42
sashoalm27-Nov-08 2:42 
QuestionSAFEARRAY [modified] Pin
Vijjuuu.26-Nov-08 23:32
Vijjuuu.26-Nov-08 23:32 
QuestionRe: SAFEARRAY Pin
Roger Stoltz27-Nov-08 1:40
Roger Stoltz27-Nov-08 1:40 
AnswerRe: SAFEARRAY Pin
Vijjuuu.27-Nov-08 1:49
Vijjuuu.27-Nov-08 1:49 
AnswerRe: SAFEARRAY Pin
Roger Stoltz27-Nov-08 4:04
Roger Stoltz27-Nov-08 4:04 
GeneralRe: SAFEARRAY Pin
Vijjuuu.27-Nov-08 4:23
Vijjuuu.27-Nov-08 4:23 
Question[Message Deleted] Pin
AnithaSubramani26-Nov-08 23:19
AnithaSubramani26-Nov-08 23:19 
QuestionRe: ReadData() in serialport communication Pin
CPallini26-Nov-08 23:26
mveCPallini26-Nov-08 23:26 
AnswerRe: ReadData() in serialport communication Pin
Cedric Moonen27-Nov-08 0:15
Cedric Moonen27-Nov-08 0:15 
Questioni want make an mfc applicatio for screen capturing Pin
Dhiraj kumar Saini26-Nov-08 23:15
Dhiraj kumar Saini26-Nov-08 23:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.