Solved

How to Capture Screen ?

Posted on 1998-11-19
6
471 Views
Last Modified: 2013-11-20
Dear experts

I'm using VC++5.0 in WindowNT4.0 SP 3.
I'd like to capture the screen image programatically.
I have to save the certain area which has associated memory DC to 24 bit BMP file on any VGA color depth.
i.e. if my app run on non-true-color VGA environment, memoryDC must be changed and saved as 24 bit type.
For this, I tried as following :
 - CreateCompatibleBitmap(pMemDC,...)
 - Get 24 bit bitmap data with GetDIBits(hBitmap, ..).
But I always get empty data buffer. i.e. '0'.

Following is source code of my app.
--------
CRect rtWnd;
GetDlgItem(IDC_FRAME1)->GetClientRect(rtWnd);

CDC* pFrameWndDC;

BITMAPINFOHEADER      BmpInfoHdr;

BmpInfoHdr.biSize = sizeof(BITMAPINFOHEADER);
BmpInfoHdr.biWidth = rtWnd.Width();
BmpInfoHdr.biHeight = rtWnd.Height();
BmpInfoHdr.biPlanes = 1;
BmpInfoHdr.biBitCount = 24;
BmpInfoHdr.biCompression = BI_RGB;
BmpInfoHdr.biSizeImage = 0;
BmpInfoHdr.biXPelsPerMeter = 0;
BmpInfoHdr.biYPelsPerMeter = 0;
BmpInfoHdr.biClrUsed = 0;
BmpInfoHdr.biClrImportant = 0;

HDC hDC;
CBitmap* pBMP;
HBITMAP hBMP;

void*   pBitmapBuff = calloc((((rtWnd.Width()*3)+3)/4)*4 * rtWnd.Height() * 3, 1); // 24 Bit
      
int nTotlaFrameCount = m_lstMemDC.GetCount();

POSITION pos = m_lstMemDC.GetHeadPosition();

for(int i=0; i<nTotlaFrameCount; i++)
{
    pFrameWndDC = GetFrameDC(i);
            
    hDC = pFrameWndDC->GetSafeHdc();
    pBMP = new CBitmap;
    pBMP->CreateCompatibleBitmap(pFrameWndDC,
           (((rtWnd.Width()*3)+3)/4)*4, rtWnd.Height());
    hBMP = (HBITMAP) pBMP->GetSafeHandle();

    GetDIBits( hDC, hBMP, 0, BmpInfoHdr.biHeight,  
  pBitmapBuff, (LPBITMAPINFO) &BmpInfoHdr, DIB_RGB_COLORS);

   SaveAs24BMPFile("Test.bmp", pBitmapBuff, BmpInfoHdr);
   delete pBMP;
}
-------

Any comments will be appreciated.
0
Comment
Question by:okg
6 Comments
 

Author Comment

by:okg
ID: 1324968
Edited text of question
0
 

Author Comment

by:okg
ID: 1324969
Edited text of question
0
 

Author Comment

by:okg
ID: 1324970
Edited text of question
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 12

Expert Comment

by:migel
ID: 1324971
HI! I can`t find BitBlt call in your code. According your code you don`t  capture screen? just create empty bitmap and get it`s buffer.
To capture screen you must select created bitmap into DC and copy screen buffer to them.
for example:
{
ClentToScreen(rtWnd);
HDC hDeskDC = ::GetDC(NULL);
HBITMAP hOldBm = ::SelectObject(hDC,  hBMP);
::BitBlt(hDC, hDeskDC, 0, 0, rtWnd.Width(), rtWnd.Height(), hDeskDC, rtWnd.Left, rtWnd. Top, SRCCOPY);
 ::SelectObject(hDC,  hOldBm );
::ReleaseDC(NULL, hDeskDC);
// now your hBMP contain copy of the screen image
}

0
 
LVL 6

Expert Comment

by:snoegler
ID: 1324972
I've never tried this, but maybe it works ( it works at least
with CRichEditCtrl's ...)

CWnd *pDesktopWnd=GetDesktopWindow();
CDC  *pDesktopTempDC=pDesktopWnd->GetDC();
// Create compatible DC
CDC  *pMemDC=new CDC;
CBitmap *pMemBmp=new CBitmap;
pMemDC->CreateCompatibleDC(pDesktopTempDC);
pMemBmp->CreateCompatibleBitmap(pDesktopTempDC,
                                ::GetSystemMetrics(SM_CXSCREEN),
                                ::GetSystemMetrics(SM_CYSCREEN));
// Release the temporary DC
pDesktopWnd->ReleaseDC(pDesktopTempDC);

// Select the memory bitmap
int nSDC=pMemDC->SaveDC();
pMemDC->SelectObject(pMemBmp);
// Render the desktop window into the memory bitmap
GetDesktopWindow()->SendMessage(WM_PRINT,
                                (WPARAM)pMemDC->GetSafeHdc(),
                                PRF_CLIENT|
                                PRF_ERASEBKGND|
                                PRF_CHILDREN|PRF_OWNED);
pMemDC->RestoreDC(nSDC);
delete pMemDC;

// Now pMemBmp should contain the desktops' contents.
0
 
LVL 1

Accepted Solution

by:
atari earned 100 total points
ID: 1324973
Again my code snippet, it worked, I tested it yesterday 11/18/1998:

Tried to capture screen and presented in my mdi-view:

void CMyView::OnCaptureScreen()
{
  CClientDC clientDC(this); // get my dc
  CRect windowrect;
  GetClientRect(&windowrect);
 
  HDC screenDC=::GetDC(NULL); // get desktop dc
  CDC* dc = CDC::FromHandle(screenDC);
  CRect rectDesktop;
  if (!::SystemParametersInfo(SPI_GETWORKAREA,0,&rectDesktop,0))
        ::GetWindowRect(::GetDesktopWindow(),&rectDesktop);
  else
  {
      rectDesktop.right=::GetSystemMetrics(SM_CXSCREEN);
      rectDesktop.bottom=::GetSystemMetrics(SM_CYSCREEN);
      rectDesktop.right-=rectDesktop.left;
      rectDesktop.bottom-=rectDesktop.top;
      rectDesktop.left=-rectDesktop.left;
      rectDesktop.top=-rectDesktop.top;
  }
  CBitmap bitmap;
  bitmap.CreateCompatibleBitmap(dc,rectDesktop.Width(),rectDesktop.Height());
  CDC memDC;
  CBitmap *pOldBitmap;
  memDC.CreateCompatibleDC(dc);
  pOldBitmap=memDC.SelectObject(&bitmap);
  memDC.BitBlt(0,0,rectDesktop.Width(),rectDesktop.Height(),dc,0,0,SRCCOPY);
  clientDC.StretchBlt(windowrect.left,windowrect.top,windowrect.Width(),windowrect.Height(),&memDC,0,0,rectDesktop.Width(),rectDesktop.Height(),SRCCOPY);
  memDC.SelectObject(pOldBitmap);
}

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 124
ODBC Connection Logging, ADO.NET 6 64
conditional code and condition difference 9 82
sum13 challenge 24 106
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question