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);
}
}