Click here to Skip to main content
12,633,990 members (24,215 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.1K views
506 downloads
13 bookmarked
Posted

Screenshot Program

, 24 Jun 2012 CPOL
Rate this:
Please Sign up or sign in to 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 

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. 

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

License

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

Share

About the Author

Semertzidis Aris
Software Developer
Greece Greece
http://semergeegee.blogspot.com

You may also be interested in...

Pro

Comments and Discussions

 
QuestionProgram crash on Win 7 32bit Pin
Kiran Satish2-Jul-12 11:07
memberKiran Satish2-Jul-12 11:07 
GeneralRe: Program crash on Win 7 32bit Pin
Southmountain29-Nov-15 6:37
memberSouthmountain29-Nov-15 6:37 
QuestionThank You Pin
Patrick Harris26-Jun-12 19:57
memberPatrick Harris26-Jun-12 19: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 24 Jun 2012
Article Copyright 2012 by Semertzidis Aris
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid