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

Screenshot Program

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
24 Jun 2012CPOL 16.3K   637   14   3
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 

Screenshot

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. 

C#
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.

C#
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. 

C#
//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;
}  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Greece Greece
http://semergeegee.blogspot.com

Comments and Discussions

 
QuestionProgram crash on Win 7 32bit Pin
Kiran Satish2-Jul-12 10:07
Kiran Satish2-Jul-12 10:07 
GeneralRe: Program crash on Win 7 32bit Pin
Southmountain29-Nov-15 5:37
Southmountain29-Nov-15 5:37 
QuestionThank You Pin
Patrick Harris26-Jun-12 18:57
Patrick Harris26-Jun-12 18:57 

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.