Click here to Skip to main content
6,629,377 members and growing! (20,427 online)
Email Password   helpLost your password?
Languages » C# » Applications     Beginner License: The Code Project Open License (CPOL)

TeboScreen: Basic C# Screen Capture Application

By GuyThiebaut

A C# screensaver application to illustrate simple screen capture principles
C# (C# 1.0, C# 2.0, C# 3.0), Dev, Design
Posted:6 Dec 2007
Updated:18 Dec 2007
Views:33,275
Bookmarked:69 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 4.76 Rating: 4.41 out of 5

1

2
1 vote, 9.1%
3
4 votes, 36.4%
4
6 votes, 54.5%
5
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 releasing the left mouse button, the user selects a file name and the area behind the drawn rectangle is captured to this file.

Background

This application was an exercise I set myself in C#. Being new to C#, I wanted to see how straightforward creating a screensaver application would be. In the end, it took me an afternoon to write the application (not too onerous). There are many possibilities for future development.

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.

Using the Code

There are two modes of screen capture:

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));
ScreenShot.CaptureImage(Point.Empty, Point.Empty, bounds, ScreenPath);

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 selects a file name and what is behind the drawn rectangle is captured to this selected file. The mouse_Move event is used to erase the previous rectangle and to create a new rectangle every time the mouse is moved (while the left mouse button is held down). This gives the illusion of a rectangle being stretched.

private void mouse_Move(object sender, MouseEventArgs e)
        {

        //Resize (actually delete then re-draw) the rectangle if the left 
        mouse button is held down
        if (LeftButtonDown)
            {

            //Erase the previous rectangle
            g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, 
CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y - 
CurrentTopLeft.Y);

            //Calculate X Coordinates
            if (Cursor.Position.X < ClickPoint.X)
                {
                CurrentTopLeft.X = Cursor.Position.X;
                CurrentBottomRight.X = ClickPoint.X;
                }
            else
                {
                CurrentTopLeft.X = ClickPoint.X;
                CurrentBottomRight.X = Cursor.Position.X;
                }

            //Calculate Y Coordinates
            if (Cursor.Position.Y < ClickPoint.Y)
                {
                CurrentTopLeft.Y = Cursor.Position.Y;
                CurrentBottomRight.Y = ClickPoint.Y;
                }
            else
                {
                CurrentTopLeft.Y = ClickPoint.Y;
                CurrentBottomRight.Y = Cursor.Position.Y;
                }

            //Draw a new rectangle
            g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, 
CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y - 
CurrentTopLeft.Y);

            }

        }

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

Point StartPoint = new Point(ClickPoint.X, ClickPoint.Y);
Rectangle bounds = new Rectangle(ClickPoint.X, ClickPoint.Y, 
    CurrentPoint.X - ClickPoint.X, CurrentPoint.Y - ClickPoint.Y);
ScreenShot.CaptureImage(StartPoint, Point.Empty, bounds, ScreenPath);

The code that captures the screen is in a class called ScreenShot which contains a static method called CaptureImage.

class ScreenShot
     {
     public static void CaptureImage(Point SourcePoint, Point DestinationPoint, 
         Rectangle SelectionRectangle, string FilePath)
         {
         using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, 
             SelectionRectangle.Height))
             {
             using (Graphics g = Graphics.FromImage(bitmap))
                 {
                 g.CopyFromScreen(SourcePoint, DestinationPoint, 
                     SelectionRectangle.Size);
                 }
             bitmap.Save(FilePath, ImageFormat.Bmp);
             }
         }
     }

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

License

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

About the Author

GuyThiebaut


Member
Graduated with BSc(hons) in Information Systems in 1992 and have had to learn pretty much everything on the job since then.

Currently: Business Analyst (Oracle, SAS, Excel, C#...)
Used to be: SQLServer 2000 DBA, Microsoft XAL developer

SQL Server,C# VB .NET
Access, Excel, Word - VBA, SAS


Interests include keeping gerbils, reading and things that fly.
Was once a classical guitarist, however Metallica have corrupted my tastes.
Occupation: Database Developer
Location: United Kingdom United Kingdom

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
Generalonly for windows application Pinmembernithyasajil21:35 7 Oct '09  
GeneralGood work Pinmemberrasheed19792:46 2 May '09  
GeneralRe: Good work PinmemberGuyThiebaut3:24 2 May '09  
GeneralDoes not capture whole image [modified] PinmemberMember 37199504:17 22 Apr '09  
GeneralRe: Does not capture whole image PinmemberGuyThiebaut8:25 23 Apr '09  
GeneralRead the text from the screen PinmemberMember 391808821:06 2 Jun '08  
GeneralA couple of suggestions PinmemberDjDanny23:49 12 Dec '07  
GeneralRe: A couple of suggestions [modified] PinmemberGuyThiebaut0:58 13 Dec '07  
GeneralRe: A couple of suggestions PinmemberLiam O'Hagan14:08 3 Jan '08  
GeneralSelecting an area PinmemberLiam O'Hagan16:13 9 Dec '07  
GeneralRe: Selecting an area PinmemberGuyThiebaut1:00 10 Dec '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Dec 2007
Editor: Genevieve Sovereign
Copyright 2007 by GuyThiebaut
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project