Window Screen Capture from a C++ program.

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.
biff999Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jkrConnect With a Mentor Commented:
>>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
 
grg99Commented:
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
 
jkrCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
grg99Commented:
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
 
AxterCommented:
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
 
AxterCommented:
Writing a window image to a BMP file
http://www.codeguru.com/Cpp/G-M/bitmap/article.php/c1741/
0
 
AxterCommented:
FYI:
The link I just posted does not save the Palette info, if the display is set for true color.
0
 
AxterCommented:
0
 
grg99Commented:
( 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
 
AxterCommented:
>>( 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
 
biff999Author Commented:
Thank you everyone for all of your comments.
0
 
jkrCommented:
You are accepting an answer that says "Do a google search"? Please explain...
0
 
jkrCommented:
BTW: Without an explanation, I will ask Customer Service to have a look into this.
0
 
AxterCommented:
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
 
biff999Author Commented:
Is everyone happy now?
0
 
grg99Commented:
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
 
AxterCommented:
>>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
 
AxterCommented:
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
 
jkrCommented:
>>Is everyone happy now?

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

From novice to tech pro — start learning today.