Click here to Skip to main content
Click here to Skip to main content
Go to top

TeboScreen: Basic C# Screen Capture Application

, 18 Feb 2012
Rate this:
Please Sign up or sign in to vote.
A C# screensaver application to illustrate simple screen capture principles
Screenshot -

Introduction

This application captures the screen in two different ways:

  • Capture Screen: This does what is says; it basically captures the whole of the screen once the file name of the resulting image has been specified.
  • Capture Area: Holding down the left mouse button, users draw a rectangle specifying which part of the screen they wish to capture. On double clicking, the user selects a file name and the area behind the drawn rectangle is captured to this file. Once drawn, the rectangle can be resized and moved around the screen before double clicking to save the image.

Background

This application was an exercise I set myself in C#. Being new to C#, I wanted to see how straightforward creating a screen capture application would be. In the end, it took me an afternoon to write the application (not too onerous).

I decided on a simple solution to the question of how do you allow the user draw a rectangle on a screen.
The application maximizes a form which has a transparency of 30%. The drawing of the rectangle involves drawing a rectangle and then erasing the previous rectangle by drawing a rectangle in the form's background colour.
As the form is maximized, any coordinates on this form correspond to screen coordinates underneath.
It is then a simple matter of returning the coordinates of the drawn rectangle to the ScreenShot.CaptureImage method.

Some additional features are the ability to save the image to the clipboard, include the cursor in the image (in this case, by pressing the 'S' key) and to enable the user to resize and drag the selection area once the initial selection area has been drawn.

Using the Code

There are two modes of screen capture - capture screen and capture area.

Capture Screen

All we need to do is pass the screen area to the ScreenShot.CaptureImage method. The only thing to note here is that we pause for 250 milliseconds to allow the screen to repaint itself. Not doing this can cause the form, from which the command was invoked, to be included in the capture even though it has been instructed to minimize.

//Allow 250 milliseconds for the screen to repaint itself 
//(we don't want to include this form in the capture)
System.Threading.Thread.Sleep(250);

Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));
string fi = "";

if (ScreenPath != "")
{
    fi = new FileInfo(ScreenPath).Extension;
}

ScreenShot.CaptureImage(showCursor, curSize, curPos, 
    Point.Empty, Point.Empty, bounds, ScreenPath, fi);

Capture Area

Holding down the left mouse button, the user draws a rectangle specifying which part of the screen they wish to capture. On releasing the left mouse button, the user can re-size or move the selection area or they can double click to select a file name - what is behind the drawn rectangle is then captured to this selected file.

The mouse_Move event is used to decide on whether the user is drawing, dragging(moving) or resizing the selection area.
Each of the methods erases the previous rectangle and to creates a new rectangle every time the mouse is moved (while the left mouse button is held down). This gives the illusion of a rectangle moving.

private void mouse_Move(object sender, MouseEventArgs e)
{
    if (LeftButtonDown && !RectangleDrawn)
    {
        DrawSelection();
    }

    if (RectangleDrawn)
    {
        CursorPosition();

        if (CurrentAction == ClickAction.Dragging)
        {
            DragSelection();
        }

        if (CurrentAction != ClickAction.Dragging && CurrentAction != ClickAction.Outside)
        {
            ResizeSelection();
        }
    }
}

Here is how we call ScreenShot.CaptureImage for Capture Area.

Point StartPoint = new Point(CurrentTopLeft.X, CurrentTopLeft.Y);
Rectangle bounds = new Rectangle(CurrentTopLeft.X, CurrentTopLeft.Y, 
    CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y - CurrentTopLeft.Y);
...
ScreenShot.CaptureImage(showCursor, curSize, curPos, StartPoint, 
        Point.Empty, bounds, ScreenPath, fi);

The code that captures the screen is in a class called ScreenShot which contains a static method called CaptureImage.
This is where the code applies all the selected options - include/exclude cursor, save to clipboard/file and type of file the image is saved to.

class ScreenShot
    {
        public static bool saveToClipboard = false;

        public static void CaptureImage(bool showCursor, Size curSize, Point curPos, 
    Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, 
    string FilePath, string extension)
        {
            using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, 
        SelectionRectangle.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(SourcePoint, DestinationPoint, 
            SelectionRectangle.Size);

                    if (showCursor)
                    {
                        Rectangle cursorBounds = new Rectangle(curPos, curSize);
                        Cursors.Default.Draw(g, cursorBounds);                            
                    }
                }

                if (saveToClipboard)
                {
                    Image img = (Image)bitmap;
                    Clipboard.SetImage(img);
                }
                else
                {
                    switch (extension)
                    {
                        case ".bmp":
                            bitmap.Save(FilePath, ImageFormat.Bmp);
                            break;
                        case ".jpg":
                            bitmap.Save(FilePath, ImageFormat.Jpeg);
                            break;
                        case ".gif":
                            bitmap.Save(FilePath, ImageFormat.Gif);
                            break;
                        case ".tiff":
                            bitmap.Save(FilePath, ImageFormat.Tiff);
                            break;
                        case ".png":
                            bitmap.Save(FilePath, ImageFormat.Png);
                            break;
                        default:
                            bitmap.Save(FilePath, ImageFormat.Jpeg);
                            break;
                    }
                }
            }
        }

Two methods that have been added recently (January 2012) are Clipboard.SetImage and Cursors.Default.Draw. The first method will copy the screen or area captured to the clipboard and the second method includes the cursor (as an arrow) in the image.
The addition of the cursor can be particularly useful if you need to point something out in a screenshot such as a button.

To include the cursor in the image, the application's KeyUp events listen for the 'S' key - this then triggers the ScreenCapture method specifying the inclusion of the cursor.
I have added this event to each button on the application - a more efficient way to do this would be to handle any keys that are pressed while the application is open - the reason I have chosen this method is so that the software is not flagged as key logging by security software.

private void keyTest(KeyEventArgs e)
{
    if (e.KeyCode.ToString() == "S")
    {
      screenCapture(true);
    }
}

Both the addition of the cursor and clipboard feature are from requests, so please do keep the suggestions and requests coming in as I do tend to act on them.

I am also aware that Windows 7 now comes bundled with a snipping tool - this article was first created in 2007 before Windows 7 had been released.
I feel the article still has some relevance in terms of illustrating the drawing and moving of objects on the screen and taking screenshots.

Developments

TeboScreen has been branched into a new project and article called ScreenCap by Patrick Harris.
Amongst the extra features ScreenCap provides are:

Run in the background and become activated when the Print Screen button is pressed.
Support for dual monitors, regardless of whether there is a size difference in monitors.
Automatically send screen capture to three devices: Clipboard, Printer, and Email.

As a developer, I am very happy with this new development and I look forward to seeing where Patrick takes this new branch.

History

  • 4 December, 2007 -- Original version posted
  • 18 December, 2007 -- Downloads and article content updated
    • New download code fixes bugs in the drawing of the selection rectangle
    • Replacement article text for the mouse_Move event
  • 3 July, 2011 -- The updated code includes changes that allow the user to resize and move the image capture selection area. There is also a tips button which when clicked shows basic use instructions.
  • 28 December, 2011 -- Downloads updated
  • 19 January, 2012 -- Article and downloads updated
  • 18 February, 2012 -- An addition in order to reference Patrick Harris' ScreenCap article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

GuyThiebaut
Software Developer
United Kingdom United Kingdom
Graduated with BSc(hons) in Information Systems in 1992 and have had to learn pretty much everything on the job since then.
 
Currently: Developer (SQL Server and .Net).
Used to be: Analyst(Using Oracle, SAS, Excel...) and before that a SQLServer 2000 DBA, Microsoft XAL developer.
 
Main skill set includes SQL Server, PL/SQL, C#, VB .NET, Access, Excel, Word - VBA, SAS...
 

Interests include reading and things that fly.
I am a keen origami(paper folding) amateur.
Was once a classical guitarist, however Metallica have corrupted my tastes.
 
I run teboweb.com as a hobby

Comments and Discussions

 
QuestionProblem with Multiple Monitors PinmemberArslan Elahi10-May-14 8:14 
QuestionSomething intrigues me. PinmemberMember 1063650318-Apr-14 21:42 
QuestionDLL PinmemberMember 107193142-Apr-14 9:03 
AnswerRe: DLL PinpremiumGuyThiebaut2-Apr-14 10:05 
GeneralRe: DLL PinmemberMember 107193142-Apr-14 13:50 
GeneralRe: DLL PinpremiumGuyThiebaut2-Apr-14 20:35 
GeneralRe: DLL PinmemberMember 107193143-Apr-14 0:43 
QuestionThanks so much, helped alot PinmemberMember 812734513-Nov-13 3:27 
AnswerRe: Thanks so much, helped alot PinmemberArslan Elahi10-May-14 6:29 
QuestionThank you PinmemberGeorgeHLord4-Nov-13 16:08 
AnswerRe: Thank you PinmemberGuyThiebaut5-Nov-13 1:58 
QuestionResources for learning PinmemberProCodingKing25-Jul-13 6:19 
AnswerRe: Resources for learning PinmemberGuyThiebaut25-Jul-13 6:30 
GeneralMy vote of 5 Pinmemberwsc091819-Jun-13 15:28 
GeneralRe: My vote of 5 PinmemberGuyThiebaut20-Jun-13 6:14 
QuestionWill it Capture UAC Dialog Pinprofessionalkburman611-May-13 9:55 
QuestionScreen is not captured while display is off. Pinprofessional@AmitGajjar26-Apr-13 20:46 
AnswerRe: Screen is not captured while display is off. PinmemberGuyThiebaut26-Apr-13 20:52 
GeneralRe: Screen is not captured while display is off. Pinprofessional@AmitGajjar26-Apr-13 21:08 
QuestionStore a screenshot directly in database. PinmemberShivangDesai6-Feb-13 19:09 
AnswerRe: Store a screenshot directly in database. PinmemberGuyThiebaut7-Feb-13 3:40 
GeneralRe: Store a screenshot directly in database. PinmemberShivangDesai7-Feb-13 19:30 
GeneralRe: Store a screenshot directly in database. PinmemberGuyThiebaut7-Feb-13 20:13 
GeneralRe: Store a screenshot directly in database. PinmemberGuyThiebaut7-Feb-13 23:29 
GeneralRe: Store a screenshot directly in database. PinmemberShivangDesai10-Feb-13 18:09 
GeneralRe: Store a screenshot directly in database. PinmemberGuyThiebaut11-Feb-13 1:53 
General..................................... Pinmemberricardmag15-Jan-13 10:07 
GeneralRe: ..................................... PinmemberGuyThiebaut15-Jan-13 10:10 
QuestionCould you help me, please? Pinmemberextreme69929-Oct-12 4:49 
AnswerRe: Could you help me, please? PinmemberGuyThiebaut29-Oct-12 9:53 
GeneralRe: Could you help me, please? [modified] Pinmemberextreme69930-Oct-12 5:14 
GeneralRe: Could you help me, please? PinmemberGuyThiebaut30-Oct-12 8:34 
GeneralMy vote of 5 PinmemberFarhan Ghumra19-Jun-12 22:48 
GeneralRe: My vote of 5 PinmemberGuyThiebaut20-Jun-12 1:02 
GeneralMy vote of 5 Pinmemberchimmansrikanth24-May-12 20:16 
GeneralRe: My vote of 5 PinmemberGuyThiebaut25-May-12 8:00 
QuestionCopy the Image from Clipboard to the word document Pinmemberchimmansrikanth24-May-12 20:09 
AnswerRe: Copy the Image from Clipboard to the word document PinmemberGuyThiebaut25-May-12 8:04 
GeneralRe: Copy the Image from Clipboard to the word document Pinmemberchimmansrikanth25-May-12 18:33 
thanks 4 reply.
As we are coping the image to the clipboard of the system, cant we paste it in the word document from the clipboard using Ctrl+V keys. In the same way as we can copy the cropped image in the paint application and paste it in the word document.
 
Have a Nice day.
GeneralWONDERFUL ! ! ! ! ! ! PinmemberPatrick_SURFThru10-Apr-12 8:56 
GeneralRe: WONDERFUL ! ! ! ! ! ! PinmemberGuyThiebaut10-Apr-12 12:13 
Questionopen the croped image ina picture box Pinmemberjeffingeorge29-Mar-12 0:21 
AnswerRe: open the croped image ina picture box PinmemberGuyThiebaut29-Mar-12 0:53 
GeneralRe: open the croped image ina picture box Pinmemberjeffingeorge29-Mar-12 1:09 
GeneralRe: open the croped image ina picture box PinmemberGuyThiebaut29-Mar-12 1:52 
GeneralRe: open the croped image ina picture box Pinmemberjeffingeorge29-Mar-12 1:59 
GeneralRe: open the croped image ina picture box PinmemberGuyThiebaut30-Mar-12 5:56 
QuestionMultiple screens PinmemberThomasManz23-Jan-12 8:16 
AnswerRe: Multiple screens [modified] PinmemberGuyThiebaut23-Jan-12 8:20 
GeneralRe: Multiple screens PinmemberPatrick Harris15-Feb-12 8:44 

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 | Mobile
Web01 | 2.8.140905.1 | Last Updated 18 Feb 2012
Article Copyright 2007 by GuyThiebaut
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid