Screenshot Program





5.00/5 (1 vote)
A Windows form application, minimized in tray and waiting for specific keyboard event to create screenshots and save them as images.
Introduction
A complete application to capture screenshots
while it is working in the background and save them as images. This
project it was build on a KeyboardListener project, found in CodeProject
http://www.codeproject.com/Articles/9351/Background-applications-listening-for-keyboard-act
Also resizing the output image is available
Using the code
The first basic function is the capture of the actual screen and not of the current active window.
I have cheated a little as a trigger the 'PrintScreen' button and later i retrieve the image from the clipboard.
Image img;
private int imgNumber = 1;
private void CaptureScreen()
{
System.Windows.Forms.SendKeys.SendWait("+{PRTSC}");
img = GetImageFromClipboard();
if (ResizeCheckBox.Checked)
{
img = resizeImage(img, imageSize);
}
string _fileName = Properties.Settings.Default.SavePath + "/" + Properties.Settings.Default.FileName;
if (!File.Exists(_fileName + ".jpg"))
{
_fileName += "";
}
while(File.Exists(_fileName + ".jpg"))
{
_fileName += " - (" + imgNumber + ")";
imgNumber++;
}
img.Save(_fileName + ".jpg", ImageFormat.Jpeg);
}
// Get image From the Clipboard If there is any
public static Image GetImageFromClipboard()
{
Image clipImage = null;
if (Clipboard.ContainsImage())
{
clipImage = Clipboard.GetImage();
}
return clipImage;
}
To import KeyboardListener is too simple as it only needs an EventHandler.
public partial class ScreenShot : Form
{
KeyboardListener.s_KeyEventHandler += new EventHandler(KeyboardListener_s_KeyEventHandler);
}
private void KeyboardListener_s_KeyEventHandler(object sender, EventArgs e)
{
KeyboardListener.UniversalKeyEventArgs eventArgs = (KeyboardListener.UniversalKeyEventArgs)e;
string capturedKey = string.Format("Key = {0} Msg = {1} Text = {2}", eventArgs.m_Key, eventArgs.m_Msg, eventArgs.KeyData);
//An output example of the capturedKey string
// 'Key = 32 Msg = 257 Text = Space'
// 256 : key down 257 : key up
if (eventArgs.m_Msg == 256)
{
DoSomething();
}
else
{
DoSomething();
}
}
After all, resizing the image is not a big deal.
Create a new Bitmap
with the target size and then use the System.Drawing.Graphics
to redraw the image to fit the bitmap's size.
//Resize Image to a specific Size
private static Image resizeImage(Image imgToResize, Size size)
{
Bitmap newImage = new Bitmap(size.Width, size.Height);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(imgToResize, new Rectangle(0, 0, size.Width, size.Height));
}
return (Image)newImage;
}