Solved

Window Screen Capture from a C++ program.

Posted on 2004-04-21
20
1,383 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 22

Expert Comment

by:grg99
Comment Utility
( 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>( 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
Comment Utility
Thank you everyone for all of your comments.
0
 
LVL 86

Expert Comment

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

Expert Comment

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

Expert Comment

by:Axter
Comment Utility
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
Comment Utility
Is everyone happy now?
0
 
LVL 22

Expert Comment

by:grg99
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
>>Is everyone happy now?

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now