Click here to Skip to main content
15,868,419 members
Articles / Programming Languages / C#
Tip/Trick

NinjaSnipper - Fast Screenshot Capturing for Windows

Rate me:
Please Sign up or sign in to vote.
4.71/5 (5 votes)
30 Sep 2012GPL34 min read 24.8K   1.8K   22   6
Productivity oriented snipping tool for Windows

Introduction

There are many screenshot/screensnipping tools out there and none of them performed as I needed them. Nevertheless I gathered impressions and decided to compile the best ideas into a product, I am confident that you will like it.

My requirements for a snipping tool (all implemented) are as follows:

  1. Be able to select part of the screen with your mouse and save it to a file and/or copy it to the clipboard.
  2. Also detect pressing the PrintScreen key and save the clipboard contents to a file.
  3. Save the screenshots automatically to some predefined location with a default image format (not open a dialog every time, nor an editor)
  4. However be able to specify a different location and image format every now and then.
  5. No GUI. I wanted it to run in the background (in this case, the system tray).
  6. Activate it via keyboard shortcut. It is a deal breaker if you have to navigate to the app for every screenshot, when you are making a lot of them.
  7. Work on multiple monitors!

Background

This is more of a Tips n' Tricks entry. I will only be addressing key problems/solutions with brief descriptions and references where I see fit. Not all of the requirements will be discussed as not all of them present a "challenge" to the programmer.

For the project, I decided to use C# as it seems the most appropriate choice for a Windows application. Furthermore for the PrintScreen keys I was going to use hooks, which is of course OS specific, so there was no way to make a OS portable application in a short time. Having said that, the application has only been tested on Windows 7 64bit with .NET 4 and Vista 32bit. The executables attached to this tip may not work on other platforms.

I expect the source code to be portable to older versions of .NET and Windows. Feedback would be appreciated.

Using the Code

There are several classes, each dedicated to a specific task. There are several secondary classes, such as the Settings Form, Help Form, etc. It is not the purpose of this tip to discuss how the application is built so this is as much as I will say about the code structure.

The SnippingTool that comes with Windows 7 has a really nice interface. Once you start it, it dims the screen and you can draw a transparent rectangle on the screen, marking the area you want to copy. One way to do this is to create a form with no controls, maximize it, draw on it with the mouse, pick the coordinates and take a screen shot. Really, a straightforward task! However when you have multiple monitors, the form would maximize only on one of them. So I needed to resize the form manually. The Virtual Screen is the (rectangular) area that includes all the monitors.

You can get the origin and the size like this:

C#
SystemInformation.VirtualScreen.X

The origin may have negative coordinates. Beware when calculating! The resizing presented me with a challenge due to my lack of understanding! Merely setting the size and location of my form to those of the virtual screen was not enough. It did indeed resize the form but not the area on which I could draw. Maximizing the form took care of everything, but now I had to do it manually, whatever it may be that had to be done. Without going into detail (keywords being clientArea, clippingArea, ClientRectangle, etc.), the problem was solved by the following line:

C#
this.Bounds = new Rectangle(SystemInformation.VirtualScreen.X, SystemInformation.VirtualScreen.Y, 
    SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);

Drawing on the form was an easy task:

C#
Graphics g = this.CreateGraphics(); // where *this* is my form
g.DrawRectangle(new Pen(Color.Red, 3), x, y, width, height);

As for making the screenshot programmatically, I was going to go with Win32 API if I hadn't found this similar project, Tebo Basic C Screen Capture Application which reveals a neat solution:

C#
Bitmap bitmap = new Bitmap(width, height));
Graphics g = Graphics.FromImage(bitmap)
g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);

Just for the sake of completeness (although by no means can this make the tip complete) saving an image to a file:

C#
bitmap/img.Save("path\\file.name", ImageFormat.Jpeg); // or any other format 

Retrieving an image from the clipboard and setting one is as easy as this:

C#
Image img = Clipboard.GetImage();
Clipboard.SetImage(img);

For the PrintScreen hook, I blatantly copied the InterceptKeys class into my project from here.

It is pretty basic and self explanatory. Caution should be exercised when catching the KeyDown event nevertheless, as the order of propagating the event between applications is not set, and reading out the clipboard may occur before the new screenshot has been taken.

Since I was already using hooks, I just added a hotkey combination, namely Ctrl + Space + C.

Points of Interest

Places for improvement:

  • a list of default locations to save an image to
  • a list of commands/programs to be executed on capture
  • quality of pictures could be better

History

  • 2012-06-26 bug fix, files updated

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Germany Germany
Will program for food

Comments and Discussions

 
QuestionVery nice tool Pin
Ross MacFarlane12-Oct-12 4:50
Ross MacFarlane12-Oct-12 4:50 
AnswerRe: Very nice tool Pin
Zaraken12-Oct-12 11:52
Zaraken12-Oct-12 11:52 
GeneralRe: Very nice tool Pin
Ross MacFarlane14-Oct-12 23:56
Ross MacFarlane14-Oct-12 23:56 
GeneralRe: Very nice tool Pin
Zaraken15-Oct-12 13:27
Zaraken15-Oct-12 13:27 
GeneralMy vote of 4 Pin
Al-Samman Mahmoud2-Oct-12 10:22
Al-Samman Mahmoud2-Oct-12 10:22 
GeneralMy vote of 5 Pin
Suraj S Koneri30-Sep-12 23:12
Suraj S Koneri30-Sep-12 23:12 

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

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