David Zhou

David Zhou

  • NA
  • 1
  • 14.2k

Saving Clipboard PrintScreen to Image File in C#

May 31 2013 10:48 AM
Hello Everyone,

I'm a new poster here and apologize in advance if I have posted this in the wrong subforum.

I am trying to use Visual Studio C# to printscreen, then save the screen capture to a file.

Currently, I am having problems reading from the clipboard. I have tried using both of the following lines to save a screen capture to the clipboard:

SendKeys.SendWait("+{PRTSC}");
SendKeys.SendWait("{PRTSC}");

However, when I try to save the image using the following lines, I get a Null Reference Exception.

image = (Image)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);
image.Save("image.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);

I have tested if the clipboard actually contains an image, and the following lines indicate that it thinks the clipboard is empty. i.e. the program writes "Clipboard empty."


if (Clipboard.GetDataObject() == null)
{
Console.WriteLine("Clipboard empty.");
}

However, I know that it is not empty since I can paste the screen capture to paint after running the code.

I have looked at many tutorials on how to save screenshots to image files and don't know what I am doing wrong here. Any help would be much appreciated.

Thanks!

Answers (2)