none
Print Screen in C#

    Question

  • Hi,

    I want C# code(Windows Application) for the following functionality.

    When i run a exe it should get the screen shots of my screen and save it as image file. This should continue taking screen shots for every 30 sec till i close the application

    Friday, August 20, 2010 5:24 AM

Answers

  • This is the code to do one printscreen in C#

     

    using System.Drawing;  

    using System.Drawing.Imaging; 

     

    private void PrintScreen()
    
    {  
    
        Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    
        Graphics graphics = Graphics.FromImage(printscreen as Image);
    
        graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
    
        printscreen.Save(@"C:\Temp\printscreen.jpg", ImageFormat.Jpeg);
    
    }

     

     


    Ali Hamdar (alihamdar.com)
    • Marked as answer by karthi.boom Friday, August 20, 2010 7:39 AM
    Friday, August 20, 2010 7:19 AM

All replies

  • You will find sample application to capture screen shot @

    http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html

    • Proposed as answer by Sachin bhosale Friday, August 20, 2010 5:44 AM
    Friday, August 20, 2010 5:43 AM
  • This is the code to do one printscreen in C#

     

    using System.Drawing;  

    using System.Drawing.Imaging; 

     

    private void PrintScreen()
    
    {  
    
        Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    
        Graphics graphics = Graphics.FromImage(printscreen as Image);
    
        graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
    
        printscreen.Save(@"C:\Temp\printscreen.jpg", ImageFormat.Jpeg);
    
    }

     

     


    Ali Hamdar (alihamdar.com)
    • Marked as answer by karthi.boom Friday, August 20, 2010 7:39 AM
    Friday, August 20, 2010 7:19 AM
  • Hi karthi,

    Adding to  above replies , below article might be useful for you.

    http://www.csharphelp.com/2006/11/capturing-the-screen-image-using-c/

    http://www.codeproject.com/KB/cs/PrintScreen.aspx


    Thanks, Mohsin Shaikh. If you find my post is helpful for you then mark it as answer.
    Friday, August 20, 2010 7:28 AM
  • Hey ,
    I tried this out it didn't give any error but I couldn't find the saved image . I tried to change the path but it didn't work too. Any help please ?? I need this !

    using System.Drawing;  

    using System.Drawing.Imaging; 

     

    private void PrintScreen()
    
    {  
    
        Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    
        Graphics graphics = Graphics.FromImage(printscreen as Image);
    
        graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
    
        printscreen.Save(@"C:\Temp\printscreen.jpg", ImageFormat.Jpeg);
    
    }

    Saturday, October 20, 2012 9:45 AM
  • This code works fine for me.  Does c:\temp exist?  Put a break point in the call to "Save()" and see that it is hit.

    --
    Mike
    Saturday, October 20, 2012 12:14 PM
  • This no longer works for Metro Style apps.  Pretty disappointed that I have been unable to find a way to do this kind of functionality in Metro.  This kind of thing was available in early versions of Silverlight...

    Nevin Morrison

    Monday, November 12, 2012 11:43 PM
  • Fantastic response. Would like to add one function- 

    string saveDirectory = "Z:/IT/Development/ScreenShots/";
    Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    Graphics graphics = Graphics.FromImage(printscreen as Image);
    graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
    String[] filesInDirectory = Directory.GetFiles(saveDirectory);
    printscreen.Save(saveDirectory + "screenshot" + filesInDirectory.Count() + ".jpg", ImageFormat.Jpeg);
    This eliminates overwriting of the previous screenshot.
    Tuesday, September 23, 2014 8:41 PM
  • The above is a good version of the answer.  However, the user should know that other "using statements" and assembly references are needed to make it work.  I needed the following assembly references and their corresponding (good in case of ambiguity) using statements.

    using System.Windows.Forms;
    using System.Drawing;
    using System.Drawing.Image;
    using System.Drawing.Imaging;
    using System.IO;

    If you put the code in an Application that extends Application, you will have to add:

    using System.Windows.Application; //OR System.Windows.Forms.Application

    to declare explicitly what Application type you are talking about.

    Thursday, February 05, 2015 7:19 PM