Click here to Skip to main content
Click here to Skip to main content

NinjaSnipper - Fast screenshot capturing for Windows

By , 30 Sep 2012
 

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, you will like.

My requirements for a snipping tool. (all implemented) 

  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' Ticks 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 article 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 article to discuss how the application is built so this is as much 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 

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 

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

Drawing on the form was an easy task: 

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  http://www.codeproject.com/Articles/21913/TeboScreen-Basic-C-Screen-Capture-Application which reveals a neat solution: 

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 article complete) saving an image to a file: 

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: 

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

For the PrintScreen hook I blatantly copied the InterceptKeys class into my project from here http://sdaaubckp.svn.sourceforge.net/viewvc/sdaaubckp/xp-take-screenshot/?view=tar

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)

About the Author

Zaraken
Germany Germany
Member
Will program for food

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionVery nice toolmemberRoss MacFarlane12 Oct '12 - 4:50 
Hey,
 
Thanks for this tool.
 
I Found it quick to use but is there a keyboard shortcut for taking a snapshot of an area? I know we can click on the icon and it then lets us drag an area but is there a keyboard shortcut we can use instead of clicking the icon?
 
Thanks!

AnswerRe: Very nice toolmemberZaraken12 Oct '12 - 11:52 
yes, there is a shortcut, Ctrl+Shift+C
however it is a bit uncomfortable for webpages because the shortcut Ctrl+Shift scrolls your page down. I should probably change that.
It's all in the MIND . . .

GeneralRe: Very nice toolmemberRoss MacFarlane14 Oct '12 - 23:56 
Awesome, thanks for the reply!

GeneralRe: Very nice toolmemberZaraken15 Oct '12 - 13:27 
Just remembered something. For this tool the order of they keys for the keyboard shortcut doesn't matter, so you can actually press Ctrl + C + Space (instead of Ctrl+Space+C) to avoid scrolling your pages if the active window is a browser of some kind. sooo no need for me to change the combination for now.
It's all in the MIND . . .

GeneralMy vote of 4memberAl-Samman Mahmoud2 Oct '12 - 10:22 
wonderfull
GeneralMy vote of 5memberSuraj S Koneri30 Sep '12 - 23:12 
great coding Smile | :)

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 1 Oct 2012
Article Copyright 2012 by Zaraken
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid