Click here to Skip to main content
Click here to Skip to main content

Barry's Screen Capture

By , 11 Jan 2003
 

Sample Image - CapIT.jpg

Introduction

This article demonstrates the methods of capturing different portions of the screen. You have the facility to then save the captured image to a file.

Background

The idea for developing the article came to me when I had downloaded such a utility for capturing screen-shots for use in my articles like this one, on the Internet, but unfortunately the downloaded utility had too many nagging screens and obviously for a no-nags version I had to pay which I cannot afford. So the only way out was to develop one on my own, the MSDN sample WinCap could have served the purpose but the source was not available on the CD. So I started developing the article in MFC using the Win32 sample provided on the MSDN CD (WinCap). And also so as to be able to capture portions of the screen which I had done sometime ago in Visual Basic, since screen capture in Visual Basic is much easier and code for this is available in the Knowledge Base section of Visual Basic.

Features

This article has the following features.

  • Capture the Desktop
  • Capture a window
  • Capture a client area
  • Capture a control (e.g. Button)

In addition it has the following, interesting but not so useful (in my view) features:

  • Track the X and Y coordinates of the captured image
  • Trace the color code of a specific point of the captured image

Option for color and position tracking are on the MainFrame window of the system and can only be used after capturing an image.

How to use the project

Download the project source, unzip the files to some folder, build the project, in case there are no error at build-time, execute the project, a Icon of a Camera will appear in the System Tray (bar at the bottom of the screen), Right-Click on the the Icon and a menu will pop-up, select the options from the menu.

Sample Image

Pop-up Menu

Option Task performed
Capture Desktop Captures a image of the System Desktop immediately
Capture Window Prompts to click on window to be captured
Capture Client Area Prompts to click on client area to capture
Capture Control Prompts to click on control to capture
Clear Image Clears captured image
Show Window Display the CapIT System Window
Close Closes the CapIT System

In addition to the pop-up menus the CapIT System has it own main menu which cap be displayed when you select "Show Window" from the pop-up menu.

How it works

When capturing window or client area or control, the system calls the function SetCapture() and waits for the user to click on some window on the Desktop which is to be captured. After clicking on the area to be captured, the system framework call the member function void CCapITView::OnCaptureChanged(CWnd *pWnd) which calls WindowCapture(HWND hwnd) which in turn carries out the job of capturing the image (till the menu are disabled). After completion of capture the system calls ReleaseCapture() to release SetCapture(). But in the case of capturing the Desktop SetCapture() is not called, but directly calls WindowCapture(HWND hwnd) passing to it GetDesktopWindow()->m_hWnd and the system proceeds with capturing the Desktop image immediately, since there is only one Desktop area and its HWND is known to the System.

Note :

The following member functions in the class CCapITView are borrowed from the MSDN sample WinCap. Could I have written them on my own, No Way.

  • HBITMAP CopyWindowToBitmap(CWnd* wnd , HWND hWnd, WORD fPrintArea)
  • HBITMAP CopyScreenToBitmap(LPRECT lpRect)
  • BOOL PaintBitmap(HDC hDC, LPRECT lpDCRect, HBITMAP hDDB, LPRECT lpDDBRect, HPALETTE hPal)
  • void DoSize(CWnd* wnd , HBITMAP ghBitmap)
  • void SetupScrollBars(CWnd* wnd , WORD cxBitmap, WORD cyBitmap)
  • void ReallyGetClientRect(CWnd* wnd , LPRECT lpRect)
  • WORD SaveDIB(HDIB hDib, LPSTR lpFileName)
  • WORD PaletteSize (VOID FAR * pv)
  • WORD DibNumColors (VOID FAR * pv)
  • HDIB ChangeBitmapFormat(HBITMAP hBitmap, WORD wBitCount, WORD dwCompression, HPALETTE hPal, HWND hwnd)
  • HANDLE AllocRoomForDIB(BITMAPINFOHEADER bi, HBITMAP hBitmap , HWND hwnd)
  • HPALETTE GetSystemPalette(HWND hwnd)
  • int PalEntriesOnDevice(HDC hDC)

Known problems

Color of the saved image under Windows 98 is not the same as of the captured image, but I think it is nearly the same in Windows 2000.

Points of interest

The project uses a CScrollView instead of CView because if the image being captured is bigger in size (e.g. Desktop) then in the Frame Window you may not be able to capture the entire image since a CView will capture as per the CView Client Area size.

History

  • Version 1

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

About the Author

Barretto VN
India India
Member
Nothing to boast about

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: How to capture a hidden windowmemberfaz793 Jun '05 - 1:00 
I use this code, i don't know if it works in minimized... TRY it
memoryImage is the Bitmap you want....
 
Example
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void Capture()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
 
By FAZE

GeneralRe: How to capture a hidden windowmemberkajok3 Oct '06 - 6:27 
Nope. seems like you still have to bring the window to front to capture its contents.
 
Is there another way?
GeneralEverytime I pressed Capture desktop, it eats 4 MB of memory..memberMr. Jigar Mehta2 Jan '04 - 2:47 
Everytime Ipress Capture desktop, it eats 4 MB of memory according to my resolution 1184*764... When I do it repeatedly (Capture and Clear) it empties my memory.. Can you remove (Barry - for you!!!) that thing as everytime I test the program, I got out of memory...Frown | :(
 
Jigar Mehta
jigar@programmer.net
QuestionHow to Capture Screen with DirectX ?memberhiver6 Dec '03 - 3:27 
use GDI method is too slow, who know how to use DirectX capture screen
thanks!
 
hiver
AnswerRe: How to Capture Screen with DirectX ?memberKovari Attila15 Feb '04 - 21:42 
i am trying to capture the screen with directx to but till now i had no luck Frown | :(
GeneralRe: How to Capture Screen with DirectX ?memberm_idris8 Jun '04 - 19:17 
I am able to capture the desktop but the rate of capturing is very slow. If any have idea pls share with me, how to increase the rate of capturing. I am using GetFrontBufferData(). This fun is slow in both mem & CPU usage (100%).

GeneralWill try the code, save me a lot of time.memberw0705310 Jul '03 - 11:47 
Laugh | :laugh: Laugh | :laugh:
GeneralRe: Will try the code, save me a lot of time.memberMr. Jigar Mehta4 Jan '04 - 23:02 
Have you tried... the code (barry's screen capture..) I have the problem with it so i am asking that...
 
Jigar Mehta
jigar@programmer.net
GeneralRe: Will try the code, save me a lot of time.memberw070536 Jan '04 - 8:31 
Yes, I have tried it, it works fine for me.
GeneralSaving to JPGmemberbob_brown_200025 Apr '03 - 18:52 
Great program - can you help extend this to save in jpg ?
 
Robert
GeneralRe: Saving to JPGmemberThatsAlok20 Jul '04 - 23:45 
Use Cximage Library to convert the bitmap produce by this project to Jpeg,or other format like gif,png etc.
 
now question is where to find CxImage,so here is the link.
http://www.codeproject.com/bitmap/cximage.asp
 
-----------------------------
"I Think It will Work"
Formerly Known As "Alok The Programmer" at CP
-----------------------------
Alok Gupta
visit me at http://www.thisisalok.tk
GeneralRe: Saving to JPGmemberBarretto VN28 Jul '04 - 1:07 
Thanks Alok
 
you seem to be doing part of my job, thanks for the same, but i found that there is a very simple way of creating images of other formats from a .bmp file, here is how it can be done (not programatically)
 
Open the saved .bmp file in Paintbrush (.exe). from the file menu select "Save As..." , in "Save As type" pull-down select the type of image you want to save / create give a name (maybe a different one or same) and click Ok.
 
Note : this works with Windows 2000 / XP , not in WIndows 98
 
Barretto VN
 

GeneralRe: Saving to JPGmemberThatsAlok28 Jul '04 - 3:19 
Actually,
i think he want to change the format of image programmatically.that why i refer to him for CXImage Library.
 
thanks for reply.any way what r u doing sir
 
-----------------------------
"I Think It will Work"
Formerly Known As "Alok The Programmer" at CP
-----------------------------
Alok Gupta
visit me at http://www.thisisalok.tk
GeneralRe: Saving to JPGmemberBarretto VN2 Aug '04 - 1:17 
ThatsAlok wrote:
thanks for reply.any way what r u doing sir
 
Nothing !!!, hav'nt you seen my profile
GeneralGood Program.memberSeungju Choi13 Jan '03 - 19:44 
All honor to your effort
Thanks.
Smile | :)
GeneralRe: Good Program.membereegiboo12 May '03 - 16:11 
nice app. thanks Smile | :)
GeneralRe: Good Program.sussAnonymous12 Apr '05 - 19:08 
thank you very much.are u from china
Generalgood article, any equivalent in c# and vs7memberelwolv12 Jan '03 - 5:16 
any equivalent to this article in c# and vs7
how to translate window handle from win 32 to managed c#?
this sould be a totally different approach to screen capture to include in a
c# desk top program.
 
elwolvSmile | :)
 
elwolv

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 12 Jan 2003
Article Copyright 2003 by Barretto VN
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid