Mohamed Mahir
Mohamed Mahir

Reputation: 91

Clipboard is always null

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

Upvotes: 1

Views: 1851

Answers (1)

Cornea Ali
Cornea Ali

Reputation: 148

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

Upvotes: 7

Related Questions