1

I am trying to take a screenshot of my monitor, after pressing Print Screen button. With the code below I am trying to check whether there is any contents in clipboard. If so, I am trying to save it in c:\ folder. But Clipboard.ContainsImage() always returns 0, but when I paste in Paint, there is an image.

I read somewhere that this can be done with delegates. Please let me know how to do that.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace Example2
{
    class Program
    {
        static void Main(string[] args)
        {
            if (Clipboard.ContainsImage())
            {
                //System.Drawing.Image screenshot = new System.Drawing.Image();
                Image screenshot = Clipboard.GetImage();

                screenshot.Save("c:\\screenshot.jpg");
            }
            Console.ReadLine();
        }
    }
}
Wolfram
  • 8,044
  • 3
  • 45
  • 66
Mohamed Mahir
  • 91
  • 1
  • 7
  • 3
    If the programming is already running before you press PrntScrn Than it can never capture the clipboard contents, because the method was already fired before the clipboard had any data. If you are opening the program afterwards it should be able to retrieve the contents of the clipboard as long as the clipboard hasn't been cleared yet. So explain us when exactly you are trying to retrieve the clipboard data (E.g. before or after the start of the program.) – woutervs Jan 07 '14 at 12:32
  • Does it work if you add [STAThread] before your Main method (See https://stackoverflow.com/questions/13571426/how-can-i-copy-a-string-to-clipboard-within-my-console-app-without-adding-a-refe) – sgmoore Jan 07 '14 at 12:34
  • @woutervs: i'm running the program only after clicking the PrntScrn – Mohamed Mahir Jan 07 '14 at 12:42
  • @sgmoore: i checked this but am not understanding it.. am just a beginner in c# – Mohamed Mahir Jan 07 '14 at 12:43

1 Answers1

7

The problem is that you are trying to access the clipboard from a console application but if you would try it from within a form this will work. Also you can make the above code work if you put this Attribute.

[STAThread()]
static void Main(string[] args)
{

Use this as a reference : Clipboard.getImage not working inside a thread

Community
  • 1
  • 1
Cornea Ali
  • 148
  • 7
  • after adding the above changes, new error "A generic error occurred in GDI+." has occured in the line "screenshot.Save("c:\\screenshot.jpg");" – Mohamed Mahir Jan 07 '14 at 13:04
  • You didn't specify the fileformat, typically microsoft tends to save everything as bmp, change your code to => screenshot.Save(@"c:\screenshot.png",System.Drawing.Imaging.ImageFormat.JPEG); – woutervs Jan 07 '14 at 13:27
  • Thank you everyone. The final exception was due to permission issues with the C folder. But now i changed the folder to "f:/" and its working now. – Mohamed Mahir Jan 07 '14 at 18:22