Solved

Window Screen Capture from a C++ program.

Posted on 2004-04-21
20
1,463 Views
Last Modified: 2012-08-13
How do I write a C++ Windows Application that takes a screen shot of the currently active window and saves it to a file?  In other words, I want to be able to do something like <ALT-PRINT SCREEN> from within a C++ program and then save it to a file.
0
Comment
Question by:biff999
  • 8
  • 5
  • 4
  • +1
20 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 10880595
Not easy, I've been working on the same thing for days.  It's easy if the window is in your application, much harder in general.

Do a google search "window contents capturing" for some starters.
0
 
LVL 86

Expert Comment

by:jkr
ID: 10880724
Check out

http://www.codeproject.com/tools/screen_snaper.asp ("Fast screen, window, region and print screen capture")
http://www.codeproject.com/dialog/screencap.asp ("Various methods for capturing the screen")
http://www.codeproject.com/bitmap/screencapture.asp ("screen capture to the Clipboard ")
0
 
LVL 22

Expert Comment

by:grg99
ID: 10880802
I was talking about getting the WHOLE window contents, even parts offscreen or under other widows.  Getting the visible desktop is easy.


http://www.codeproject.com/tools/screen_snaper.asp ("Fast screen, window, region and print screen capture")

IIRC this one has no source code.



http://www.codeproject.com/dialog/screencap.asp ("Various methods for capturing the screen")

This just discusses getting a screenshot


http://www.codeproject.com/bitmap/screencapture.asp ("screen capture to the Clipboard ")

Looks like its only for Windows in your program.



0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 30

Expert Comment

by:Axter
ID: 10880821
http:://www.axter.com/code/PrintWnd.h
http:://www.axter.com/code/PrintWnd.cpp

Using the code in the above links, you can use the following to write a window to a file:

HWND activewnd = ::GetForegroundWindow();
if (activewnd)
{
      WriteWindowToDIB("c:\\test.dib", activewnd);
}

0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 10880925
>>http://www.codeproject.com/tools/screen_snaper.asp ("Fast screen, window, region and print screen capture")
>>IIRC this one has no source code.

http://www.codeproject.com/tools/Screen_Snaper/ScreenSnaperDemo.zip

>>http://www.codeproject.com/dialog/screencap.asp ("Various methods for capturing the screen")
>>This just discusses getting a screenshot

Well, with a little editing, it is

Void CaptureScreen(HWND hCaptureWnd)
{
    int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
    int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
    HDC hDC = GetDC(hCaptureWnd);
    HDC hCaptureDC = CreateCompatibleDC(hDC);
    HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDC,
        nScreenWidth, nScreenHeight);
    SelectObject(hCaptureDC,hCaptureBitmap);
    BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,hDC,0,0,SRCCOPY);
    SaveCapturedBitmap(hCaptureBitmap); //Place holder - Put your code
                                //here to save the captured image to disk
    ReleaseDC(hCaptureWnd,hDC);
    DeleteDC(hCaptureDC);
    DeleteObject(hCaptureBitmap);
}


>>http://www.codeproject.com/bitmap/screencapture.asp ("screen capture to the Clipboard ")
>>Looks like its only for Windows in your program.

Better than "Ask Google". What is your point?
0
 
LVL 30

Expert Comment

by:Axter
ID: 10881010
Writing a window image to a BMP file
http://www.codeguru.com/Cpp/G-M/bitmap/article.php/c1741/
0
 
LVL 30

Expert Comment

by:Axter
ID: 10881033
FYI:
The link I just posted does not save the Palette info, if the display is set for true color.
0
 
LVL 30

Expert Comment

by:Axter
ID: 10881080
0
 
LVL 22

Expert Comment

by:grg99
ID: 10881118
( My point was, if you want ALL of a window, including what may be under other windows, or off-screen, then it's a bit dicy. )

Those links you gave may be perfect for the original poster.  I was just trying to warn him away from possible disappointment if he needed something more general.



0
 
LVL 30

Expert Comment

by:Axter
ID: 10881655
>>( My point was, if you want ALL of a window, including what may be under other windows, or off-screen, then it's a bit dicy. )

Are you referring to a list or a treeview window?
0
 

Author Comment

by:biff999
ID: 10890291
Thank you everyone for all of your comments.
0
 
LVL 86

Expert Comment

by:jkr
ID: 10890316
You are accepting an answer that says "Do a google search"? Please explain...
0
 
LVL 86

Expert Comment

by:jkr
ID: 10890344
BTW: Without an explanation, I will ask Customer Service to have a look into this.
0
 
LVL 30

Expert Comment

by:Axter
ID: 10891099
biff999,
This seems to indicate that telling you to do a Google search was more helpful then all the links provided.

Is that truely your take point???
0
 

Author Comment

by:biff999
ID: 10948235
Is everyone happy now?
0
 
LVL 22

Expert Comment

by:grg99
ID: 10948643
I really don't care that much, but my suggestion of "google search"  included PARTICULAR KEY WORDS that lead to a site that has a running example .exe, example source code, a detailed essay on what and how it does what it does,  for grabbing ANY window in ANY program, whether it's minimized, maximized, partially off-screen, all on-screen, rendered in any bit-depth, in the foreground, in the background, invisible, hidden in the system tray, and you get a bitmap that is in ANY bit depth, not just the screen bit depth.

Not only that, I've personally downloaded the code and incorporated it into a program, so I can vouch for the correctness of the code.

Unlike some other references that do much much much less.

Not that I really care,  much, if I get the points.

Regards,

grg99

0
 
LVL 30

Expert Comment

by:Axter
ID: 10949173
>>included PARTICULAR KEY WORDS that lead to a site that has a running example .exe, example source code, a
>>detailed essay on what and how it does what it does

IMHO, the correct approach should have been to post that link, and then add recommendation to also do a google search.
0
 
LVL 30

Expert Comment

by:Axter
ID: 10949344
grg99,
I'm only speaking for myself, but I really get irritated when I post a question, and someone answers with “Do a Google search”.
To me it’s the equivalent of asking a coworker how to spell a word, and getting an answer “Look it up in the dictionary”.
I know I can look it up in the dictionary, and I don’t need my coworker to tell me that.

Just as I know I can do a Google search, and I don’t need another expert to tell me that.
I post questions in the hope that an expert knows the answer, and can save me the time of having to search through a search engine.

In rereading your comment I see now that you’re giving a little more information then just “Do A Google Search”.  However, it would be far less ambiguous if you had posted the link directly.
It’s also better for a PAQ to have a link directly, because over time, a Google search may not return the same results.

In the future, please be kind enough to post the actual link so as to avoid ambiguity and to increase the odds that the PAQ will retain valid information.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11001554
>>Is everyone happy now?

Actually, I'd rather had seen that as a 3-way split.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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