jwillmer
jwillmer

Reputation: 3790

How to get screenshot from clipboard?

I try to get a screenshot out of the clipboard. I have wrote the following code that is triggerd by the PRINT-Key:

IDataObject obj = Clipboard.GetDataObject();
var formats = obj.GetFormats();

But the "formats" are empty. All tutorials in the www tell my to do it this way but I can´t explain the above behaviour.

(I use C# withe .NET 4.0 on a Windows 7 x64)

Upvotes: 0

Views: 2263

Answers (3)

jwillmer
jwillmer

Reputation: 3790

Found out that my keylistener blocked the windows-keylistener. Which is why windows haven´t saved the screenshot in the clipboard :-(

workaround:

// get the bounding area of the screen containing (0,0)
// remember in a multidisplay environment you don't know which display holds this point
Drawing.Rectangle bounds = Forms.Screen.GetBounds(System.Drawing.Point.Empty);

// create the bitmap to copy the screen shot to
Drawing.Bitmap bitmap = new Drawing.Bitmap(bounds.Width, bounds.Height);

// now copy the screen image to the graphics device from the bitmap
using (Drawing.Graphics gr = Drawing.Graphics.FromImage(bitmap))
{
    gr.CopyFromScreen(Drawing.Point.Empty, Drawing.Point.Empty, bounds.Size);
}

Upvotes: 0

Lander
Lander

Reputation: 3437

Have you tried the Clipboard.GetImage(); method?

Here's the MSDN sample

Image GetClipboardImage()
{
    if (Clipboard.ContainsImage())
    {
         return Clipboard.GetImage();
    }
    // add error handling
}

Upvotes: 2

Grant Thomas
Grant Thomas

Reputation: 45083

Have you tried calling the GetImage method?

image = Clipboard.GetImage();

There is more information at the link provided, which also shows how to check if an image exists on the clipboard (ContainsImage) - but you might need to take some other steps, depending on when the object was written to the clipboard.

I'm not even aware of a GetFormats method and can't find one exposed by the Windows Forms, or WPF Clipboard classes.

Upvotes: 1

Related Questions