Click here to Skip to main content
11,434,799 members (46,483 online)
Click here to Skip to main content

Screen Capturing

, 24 Mar 2006
Rate this:
Please Sign up or sign in to vote.
Capture screen contents without using any Win32 API calls, just using .NET (2.0) classes.

Demo application

Introduction

There are a lot of articles about screen capturing, but most of them use Win32 API functions, even in .NET 2.0, where this is not necessary because .NET 2.0 has all classes needed to implement that.

Background

It's often necessary to get a screenshot of the entire screen or parts of it. In our applications, we often use systems with multiple monitors, requiring us to capture all of them at once or each screen separately. Just remember, the virtual screen is the complete view of all monitors attached to the system, the primary screen is the view of the main monitor, and the working screen is the same but excluding the task bar.

Using the code

The class 'ScreenCapture' supplies a couple of methods allowing to capture the complete virtual screen, the primary screen, the working screen, a specific form (complete or only the client area), and only the contents of a specific control in a form. The images captured may be used directly from your code, or they can be saved into a file or printed. There is another small helper class 'ImageFormatHandler' which is used to handle the different graphic file formats when saving the captured image.

The source

The source code files include the two classes described, and may be directly used in your code. It's built with Visual Studio 2005.

Our main capturing method looks like below:

public Bitmap[] Capture( CaptureType typeOfCapture )
{
    // used to capture then screen in memory
    Bitmap memoryImage;
    // number of screens to capture,
    // will be updated below if necessary
    int count = 1;

    try
    {
        Screen[] screens = Screen.AllScreens;
        Rectangle rc;

        // setup the area to capture
        // depending on the supplied parameter
        switch ( typeOfCapture )
        {
            case CaptureType.PrimaryScreen:
                rc = Screen.PrimaryScreen.Bounds;
                break;
            case CaptureType.VirtualScreen:
                rc = SystemInformation.VirtualScreen;
                break;
            case CaptureType.WorkingArea:
                rc = Screen.PrimaryScreen.WorkingArea;
                break;
            case CaptureType.AllScreens:
                count = screens.Length;
                typeOfCapture = CaptureType.WorkingArea;
                rc = screens[0].WorkingArea;
                break;
            default:
                rc = SystemInformation.VirtualScreen;
                break;
        }
        // allocate a member for saving the captured image(s)
        images = new Bitmap[count];

        // cycle across all desired screens
        for ( int index = 0; index < count; index++ )
        {
            if ( index > 0 )
                rc = screens[index].WorkingArea;
                // redefine the size on multiple screens

            memoryImage = new Bitmap( rc.Width, rc.Height, 
                          PixelFormat.Format32bppArgb );
            using ( Graphics memoryGrahics = 
                    Graphics.FromImage( memoryImage ) )
            {
                // copy the screen data
                // to the memory allocated above
                memoryGrahics.CopyFromScreen( rc.X, rc.Y, 
                   0, 0, rc.Size, CopyPixelOperation.SourceCopy );
            }
            images[index] = memoryImage;
            // save it in the class member for later use
        }
    }
    catch ( Exception ex )
    {
        // handle any erros which occured during capture
        MessageBox.Show( ex.ToString(), "Capture failed", 
            MessageBoxButtons.OK, MessageBoxIcon.Error );
    }
    return images;
}

The variable images used is a member of this class, and is of type Bitmap[]. The parameter typeOfCapture of this method is an enum type defined in this class, and is used to select what actually should be captured.

There is another capture method which is used to selectively capture the contents of controls, forms, or their client area. It works quite similar to the one shown above.

private Bitmap capture( Control window, Rectangle rc )
{
    Bitmap memoryImage = null;
    images = new Bitmap[1];

    // Create new graphics object using handle to window.
    using ( Graphics graphics = window.CreateGraphics() )
    {
        memoryImage = new Bitmap( rc.Width, 
                      rc.Height, graphics );

        using ( Graphics memoryGrahics = 
                Graphics.FromImage( memoryImage ) )
        {
            memoryGrahics.CopyFromScreen( rc.X, rc.Y, 
               0, 0, rc.Size, CopyPixelOperation.SourceCopy );
        }
    }
    images[0] = memoryImage;
    return memoryImage;
}

Several other methods are used to do all the work needed for saving/printing the captured image, they are all described in comments in the source code.

public void Save( String filename, 
       ImageFormatHandler.ImageFormatTypes format )

The parameter filename holds the desired name with a complete path, the extension will be substituted depending on the file type selected by the parameter format.

This methods loads some parameters depending on the file format chosen:

ImageCodecInfo info;
EncoderParameters parameters = 
  formatHandler.GetEncoderParameters( format, out info );

That's where we use the helper class ImageFormatHandler which does all that. I think this is a base which can be used for customizing to your own needs.

Changes

The actual example has been updated by some method, which can be used to capture screens from other processes; to do that, native code must be used. Then it is no longer a pure .NET 2.0 solution. We also did not test this solution with layered forms, but a bit of modification in the base capturing method should enable this.

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

Holzhauer
Web Developer
Germany Germany
No Biography provided

Comments and Discussions

 
QuestionNice Work Pin
jemang2-Jan-12 17:06
memberjemang2-Jan-12 17:06 
GeneralScreen Capturing in VB.Net Pin
Mother of 818-May-11 18:20
memberMother of 818-May-11 18:20 
GeneralCapture screen in different ways, object wise, rectangle wise etc. Pin
yogesh_softworld12313-Oct-09 1:52
memberyogesh_softworld12313-Oct-09 1:52 
GeneralThanks Pin
KennShipley19-Jun-09 11:21
memberKennShipley19-Jun-09 11:21 
GeneralJust what I was looking for Pin
Captainobvious29-Sep-08 6:37
memberCaptainobvious29-Sep-08 6:37 
Generalabout minimize the window Pin
zac_cn116-Jan-08 19:00
memberzac_cn116-Jan-08 19:00 
GeneralThanks for your work Pin
mistnick11-Jan-08 5:03
membermistnick11-Jan-08 5:03 
QuestionI need the help please Pin
aishar23-Nov-07 7:55
memberaishar23-Nov-07 7:55 
Generalcapture Window with alpha Pin
Gallo_Teo8-May-07 8:26
memberGallo_Teo8-May-07 8:26 
GeneralCapturing on local system account Pin
biszkopt11-Mar-07 6:55
memberbiszkopt11-Mar-07 6:55 
QuestionHow to ... Pin
suneel_sp6-Mar-07 20:03
membersuneel_sp6-Mar-07 20:03 
GeneralAlphablended windows Pin
fouloud9-Dec-06 23:48
memberfouloud9-Dec-06 23:48 
Generalexported screen capture function Pin
jb706040526-Sep-06 8:56
memberjb706040526-Sep-06 8:56 
AnswerRe: exported screen capture function Pin
Holzhauer26-Sep-06 20:56
memberHolzhauer26-Sep-06 20:56 
QuestionCapturing, No problem... but can you draw? Pin
John Storer II7-Sep-06 17:59
memberJohn Storer II7-Sep-06 17:59 
AnswerRe: License Information Pin
Holzhauer24-Aug-06 22:03
memberHolzhauer24-Aug-06 22:03 
QuestionLicense Information Pin
smartsarna24-Aug-06 9:26
membersmartsarna24-Aug-06 9:26 
QuestionRe: License Information Pin
Nikhil_777713-Feb-09 7:48
memberNikhil_777713-Feb-09 7:48 
GeneralCapture process screen without changing his Z-ORDER Pin
asafbenzaken2-Jul-06 8:25
memberasafbenzaken2-Jul-06 8:25 
GeneralCode used in ASP.Net Pin
as_mahadevan31-May-06 2:23
memberas_mahadevan31-May-06 2:23 
QuestionHow it is possible through ASP.NET Pin
as_mahadevan31-May-06 2:15
memberas_mahadevan31-May-06 2:15 
GeneralKurzform Pin
Dave305222-Apr-06 23:19
memberDave305222-Apr-06 23:19 
GeneralCapture MOVIE Pin
Michael Sync22-Apr-06 11:35
memberMichael Sync22-Apr-06 11:35 
GeneralVS.Net 2003 Pin
Ahmed.mb1-Apr-06 7:29
memberAhmed.mb1-Apr-06 7:29 
GeneralRe: VS.Net 2003 Pin
edy83715-Aug-06 6:03
memberedy83715-Aug-06 6:03 
Generalnette Arbeit Pin
The_Myth24-Mar-06 23:51
memberThe_Myth24-Mar-06 23:51 
GeneralLayered Windows Pin
Thomassen23-Mar-06 13:00
memberThomassen23-Mar-06 13:00 
GeneralRe: Layered Windows Pin
Holzhauer23-Mar-06 21:40
memberHolzhauer23-Mar-06 21:40 
GeneralTnx && question Pin
mikker_12322-Mar-06 12:45
membermikker_12322-Mar-06 12:45 
GeneralRe: Tnx && question Pin
Holzhauer22-Mar-06 21:07
memberHolzhauer22-Mar-06 21:07 
GeneralRe: Tnx &amp;&amp; question Pin
The_Mega_ZZTer24-Mar-06 12:36
memberThe_Mega_ZZTer24-Mar-06 12:36 
GeneralGreat piece of code Pin
junkew19-Mar-06 8:53
memberjunkew19-Mar-06 8:53 
GeneralNice work ! Pin
BillWoodruff6-Feb-06 20:57
memberBillWoodruff6-Feb-06 20:57 
GeneralOther programs window Pin
theDiver31-Jan-06 22:45
membertheDiver31-Jan-06 22:45 
GeneralRe: Other programs window Pin
Holzhauer1-Feb-06 6:57
memberHolzhauer1-Feb-06 6:57 
GeneralRe: Other programs window Pin
wduros17-Feb-06 6:33
memberwduros17-Feb-06 6:33 
GeneralRe: Other programs window Pin
Holzhauer7-Feb-06 7:35
memberHolzhauer7-Feb-06 7:35 
GeneralRe: Avoid Win32 API? You are kidding right? Pin
Mark (Code6) Belles29-Mar-06 20:19
memberMark (Code6) Belles29-Mar-06 20:19 
GeneralRe: Avoid Win32 API? You are kidding right? Pin
Holzhauer29-Mar-06 20:39
memberHolzhauer29-Mar-06 20:39 
GeneralRe: Other programs window Pin
Kirk Horne9-Mar-06 12:15
memberKirk Horne9-Mar-06 12:15 
GeneralRe: Other programs window Pin
Holzhauer9-Mar-06 22:28
memberHolzhauer9-Mar-06 22:28 
GeneralRe: Other programs window Pin
Kirk Horne10-Mar-06 6:51
memberKirk Horne10-Mar-06 6:51 
GeneralRe: Other programs window Pin
The_Mega_ZZTer24-Mar-06 12:40
memberThe_Mega_ZZTer24-Mar-06 12:40 
GeneralRe: Other programs window Pin
The Will7-Apr-06 1:34
memberThe Will7-Apr-06 1:34 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150428.2 | Last Updated 24 Mar 2006
Article Copyright 2006 by Holzhauer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid