![]() |
Languages »
C# »
Applications
Beginner
License: The Code Project Open License (CPOL)
TeboScreen: Basic C# Screen Capture ApplicationBy GuyThiebautA C# screensaver application to illustrate simple screen capture principles |
C# (C#1.0, C#2.0, C#3.0), Dev, Design
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This application captures the screen in two different ways.
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.
There are two modes of screen capture:
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);
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);
}
}
}
mouse_Move event | You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 18 Dec 2007 Editor: Genevieve Sovereign |
Copyright 2007 by GuyThiebaut Everything else Copyright © CodeProject, 1999-2010 Web18 | Advertise on the Code Project |