Click here to Skip to main content
13,190,405 members (52,318 online)
Click here to Skip to main content
Add your own
alternative version

Stats

342.6K views
93 bookmarked
Posted 5 Apr 2001

Screen Capture to the Clipboard

, 5 Apr 2001
Rate this:
Please Sign up or sign in to vote.
How do you capture the bitmap of a window? This little download shows how, and places the bitmap on the clipboard.

Introduction

One of the common questions that I find on the microsoft.public forums is "How do I get a bitmap of a window on the screen?" Well, here's a little subroutine I used extensively in writing Win32 Programming, because I realized that for the number of illustrations I wanted for the book, there was no hope of doing screen captures and then hand-editing just the control I wanted to show. So all my Explorers have a little button somewhere that performs a screen capture of the example window and drops it into the clipboard. I could then paste it into the document, or paste it into an editor and save it as a TIFF or JPEG as well.

ToClip.h:

void toClipboard(CWnd * wnd, BOOL FullWnd);

ToClip.cpp

#include "stdafx.h"
#include "toclip.h" 
/****************************************************************
*                                 toClipboard
* Inputs:
*       CWnd * wnd: Window whose contents are to be sent 
*                   to the clipboard
*       BOOL FullWnd: TRUE for entire window, 
*                     FALSE for client area
* Result: void
*       
* Effect: 
*       Copies the contents of the client area or the window
*       to the clipboard in CF_BITMAP format.
*****************************************************************/

void toClipboard(CWnd * wnd, BOOL FullWnd)
    {
     CDC dc;
     if(FullWnd)
        { /* full window */
         HDC hdc = ::GetWindowDC(wnd->m_hWnd);
         dc.Attach(hdc);
        } /* full window */
     else
        { /* client area only */
         HDC hdc = ::GetDC(wnd->m_hWnd);
         dc.Attach(hdc);
        } /* client area only */

     CDC memDC;
     memDC.CreateCompatibleDC(&dc);

     CBitmap bm;
     CRect r;
     if(FullWnd)
        wnd->GetWindowRect(&r);
     else
         wnd->GetClientRect(&r);

     CString s;
     wnd->GetWindowText(s);
     CSize sz(r.Width(), r.Height());
     bm.CreateCompatibleBitmap(&dc, sz.cx, sz.cy);
     CBitmap * oldbm = memDC.SelectObject(&bm);
     memDC.BitBlt(0, 0, sz.cx, sz.cy, &dc, 0, 0, SRCCOPY);

     wnd->GetParent()->OpenClipboard();
     ::EmptyClipboard();
     ::SetClipboardData(CF_BITMAP, bm.m_hObject);
     CloseClipboard();

     memDC.SelectObject(oldbm);
     bm.Detach();  // make sure bitmap not deleted with CBitmap object
    }

The views expressed in these essays are those of the author, and in no way represent, nor are they endorsed by, Microsoft.

Send mail to newcomer@flounder.com with questions or comments about this article.
Copyright © 1999 All Rights Reserved
www.flounder.com/mvp_tips.htm

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Joseph M. Newcomer
Retired
United States United States
PhD, Computer Science, Carnegie Mellon University, 1975
Certificate in Forensic Science and the Law, Duquesne University, 2008

Co-Author, [i]Win32 Programming[/i]

You may also be interested in...

Comments and Discussions

 
GeneralTake a snapshot of a hidden window Pin
Member 368961631-Mar-08 3:39
memberMember 368961631-Mar-08 3:39 
GeneralRe: Take a snapshot of a hidden window Pin
Joseph M. Newcomer31-Mar-08 4:02
memberJoseph M. Newcomer31-Mar-08 4:02 
GeneralRe: Take a snapshot of a hidden window Pin
Member 368961631-Mar-08 4:58
memberMember 368961631-Mar-08 4:58 
GeneralRe: Take a snapshot of a hidden window Pin
Joseph M. Newcomer31-Mar-08 7:53
memberJoseph M. Newcomer31-Mar-08 7:53 
GeneralRe: Take a snapshot of a hidden window Pin
Member 368961631-Mar-08 8:20
memberMember 368961631-Mar-08 8:20 
GeneralRe: Take a snapshot of a hidden window Pin
Joseph M. Newcomer31-Mar-08 12:43
memberJoseph M. Newcomer31-Mar-08 12:43 
GeneralCapture MineSweeper Window Pin
SwimmerDave20-Jun-06 7:32
memberSwimmerDave20-Jun-06 7:32 
GeneralRe: Capture MineSweeper Window Pin
vikrant kpr25-Jan-08 0:25
membervikrant kpr25-Jan-08 0:25 
GeneralOther bitmap formats Pin
NeWi13-Oct-05 7:14
memberNeWi13-Oct-05 7:14 
GeneralRe: Other bitmap formats Pin
NeWi13-Oct-05 7:35
memberNeWi13-Oct-05 7:35 
GeneralRe: Other bitmap formats Pin
Joseph M. Newcomer18-Oct-05 7:46
memberJoseph M. Newcomer18-Oct-05 7:46 
Generalwindows on top interfere Pin
bbrehm0430-Aug-05 16:10
memberbbrehm0430-Aug-05 16:10 
GeneralRe: windows on top interfere Pin
Joseph M. Newcomer5-Sep-05 16:07
memberJoseph M. Newcomer5-Sep-05 16:07 
GeneralRe: windows on top interfere Pin
bbrehm046-Sep-05 14:13
memberbbrehm046-Sep-05 14:13 
GeneralCapture in Win32 - code Pin
powerpop30-Jun-05 16:20
memberpowerpop30-Jun-05 16:20 
QuestionHow to send captured screen shot through a socket? Pin
bijut6-May-05 7:39
memberbijut6-May-05 7:39 
AnswerRe: How to send captured screen shot through a socket? Pin
James R. Twine6-May-05 8:18
memberJames R. Twine6-May-05 8:18 
AnswerRe: How to send captured screen shot through a socket? Pin
Joseph M. Newcomer6-May-05 11:31
memberJoseph M. Newcomer6-May-05 11:31 
AnswerRe: How to send captured screen shot through a socket? Pin
jags_vc8-May-05 22:52
memberjags_vc8-May-05 22:52 
Questionhow to get image in memory? Pin
jags_vc25-Feb-05 1:00
memberjags_vc25-Feb-05 1:00 
AnswerRe: how to get image in memory? Pin
Joseph M. Newcomer25-Feb-05 6:18
memberJoseph M. Newcomer25-Feb-05 6:18 
GeneralRe: how to get image in memory? Pin
jags_vc27-Feb-05 18:13
memberjags_vc27-Feb-05 18:13 
GeneralRe: how to get image in memory? Pin
Joseph M. Newcomer27-Feb-05 22:02
memberJoseph M. Newcomer27-Feb-05 22:02 
There are several possible answers here.

First, if you are trying to do some sort of remote control, be aware that (a) this is very, very nontrivial and (b) it is not done by capturing screen images.

By "already in memory", I presume you mean that you have used the techniques I described to actually capture the screen.

If you have a memory DC with a bitmap, you can get the bitmap image using GetBitmapBits, GetDIBits, etc. You cannot transfer it to another PC by using a memDC; you have to extract the bits from the memory DC, then send them via a socket to another computer, which is running the other side of the communication, which transfers the bits into a memDC, and then displays it on another window.

Sending a bitmap for a 1024x178 display requires a minimum of 2,359,296 bytes for 24-bit color. But not all those bytes have to be transmitted. Using compression schemes such as JPEG, GIF, or even ZIP, you can sometimes compress that image down by a factor of 20:1 (on a good day, with luck). Thus, on a good day you might only need 120K bytes. Your Mileage May Vary.

The time taken to do the transmission is, of course, a function of the network bandwidth. But don't think 100Base-T (100Mb/s) can actually transfer 100Mb/s. Concepts such as packetization mean that you will get a certain overhead every, well, 1456 bytes. You might need 85-100 packets to transmit a compressed image; five or ten times that for one that does not compress well. Add to this the transmission overhead, packet flow control, etc. and you will be lucky to get anything vaguely useful. Figure your best will be 1Mb, your worst, 18Mb. So the worst case would take, if 100% of the bandwidth could be used, about 188ms per screen. But realistically, you might expect to get one or two screens per second under worst case, over 100Base-T. For 10Base-T, that's one screen every 5 to 10 seconds. By the time you get to 1000Base-T (gigabit copper) you would hope to see 20ms/screen, but I doubt you can get that rate (that's 50 frames/sec, and I know that doesn't happen over 1000Base-T without a LOT of effort).

Dialup is ruled out entirely.
GeneralRe: how to get image in memory? Pin
jags_vc7-Mar-05 1:13
memberjags_vc7-Mar-05 1:13 
GeneralRe: how to get image in memory? Pin
Joseph M. Newcomer9-Mar-05 10:40
memberJoseph M. Newcomer9-Mar-05 10:40 
GeneralAbout mouse pointer Pin
Rodrigo Vaz4-Apr-04 17:09
memberRodrigo Vaz4-Apr-04 17:09 
GeneralRe: About mouse pointer Pin
Joseph M. Newcomer4-Apr-04 19:35
memberJoseph M. Newcomer4-Apr-04 19:35 
GeneralRe: About mouse pointer Pin
Rodrigo Vaz6-Apr-04 19:25
sussRodrigo Vaz6-Apr-04 19:25 
GeneralRe: About mouse pointer Pin
Joseph M. Newcomer6-Apr-04 21:12
memberJoseph M. Newcomer6-Apr-04 21:12 
GeneralRe: About mouse pointer Pin
Rodrigo Vaz7-Apr-04 18:29
memberRodrigo Vaz7-Apr-04 18:29 
GeneralRe: About mouse pointer Pin
Andyzyx4-Sep-04 17:24
memberAndyzyx4-Sep-04 17:24 
GeneralRe: About mouse pointer Pin
Joseph M. Newcomer5-Sep-04 10:57
memberJoseph M. Newcomer5-Sep-04 10:57 
QuestionHow capture a DirectX or OpenGL window? Pin
peterbing22-Feb-04 21:17
memberpeterbing22-Feb-04 21:17 
AnswerRe: How capture a DirectX or OpenGL window? Pin
Joseph M. Newcomer22-Feb-04 22:13
memberJoseph M. Newcomer22-Feb-04 22:13 
GeneralRe: How capture a DirectX or OpenGL window? Pin
NeWi13-Oct-05 7:29
memberNeWi13-Oct-05 7:29 
Questionhow to save clipboard to file?? Pin
Anonymous1-Jan-04 20:41
sussAnonymous1-Jan-04 20:41 
AnswerRe: how to save clipboard to file?? Pin
Joseph M. Newcomer1-Jan-04 20:55
memberJoseph M. Newcomer1-Jan-04 20:55 
AnswerRe: how to save clipboard to file?? Pin
Anonymous14-Jan-04 8:08
sussAnonymous14-Jan-04 8:08 
GeneralRe: how to save clipboard to file?? Pin
Joseph M. Newcomer14-Jan-04 8:58
memberJoseph M. Newcomer14-Jan-04 8:58 
AnswerRe: how to save clipboard to file?? Pin
NeWi13-Oct-05 7:18
memberNeWi13-Oct-05 7:18 
QuestionHow to convert to non-mfc? Pin
Tommy2k9-Dec-03 11:22
memberTommy2k9-Dec-03 11:22 
AnswerRe: How to convert to non-mfc? Pin
NeWi13-Oct-05 7:25
memberNeWi13-Oct-05 7:25 
QuestionHow can i detect a global screen capture? Pin
Miguel Lopes27-Oct-03 13:38
memberMiguel Lopes27-Oct-03 13:38 
AnswerRe: How can i detect a global screen capture? Pin
Joseph M. Newcomer2-Nov-03 13:29
memberJoseph M. Newcomer2-Nov-03 13:29 
QuestionHow can I print it? Pin
Alex Evans30-Sep-03 14:32
memberAlex Evans30-Sep-03 14:32 
GeneralCapturing the whole window Pin
Arne Diegenbach25-Sep-03 11:35
sussArne Diegenbach25-Sep-03 11:35 
GeneralI rate 5 for this article! Pin
xxxyyyzzz12-Feb-03 10:17
memberxxxyyyzzz12-Feb-03 10:17 
Generalsomething that you don't need Pin
Anonymous10-Oct-02 3:44
sussAnonymous10-Oct-02 3:44 
GeneralRe: something that you don't need Pin
Joseph M. Newcomer10-Oct-02 15:23
memberJoseph M. Newcomer10-Oct-02 15:23 
Generalthere's a bug Pin
pangtao17-Jul-02 17:45
susspangtao17-Jul-02 17:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171016.2 | Last Updated 6 Apr 2001
Article Copyright 2001 by Joseph M. Newcomer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid