Solved

Window Screen Capture from a C++ program.

Posted on 2004-04-21
20
1,418 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

932 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

11 Experts available now in Live!

Get 1:1 Help Now