0

I have an MVC application where i am running windows' SnippingTool.exe to take the screenshots. Clipboard.GetImage() returns null when i am trying to get the captured image to save at a particular location.

[STAThread]
    public ActionResult AddImages(UserEntry model)
    {

        if (!Environment.Is64BitProcess)
            System.Diagnostics.Process.Start("C:\\Windows\\sysnative\\SnippingTool.exe");
        else
            System.Diagnostics.Process.Start("C:\\Windows\\system32\\SnippingTool.exe");


        IDataObject obj = Clipboard.GetDataObject(); //returns null
        Image img1 = Clipboard.GetImage();  // returns null

        return View();
    }
RoshanJha
  • 61
  • 7

1 Answers1

0

The issue is that you start SnippingTool and instant right after that you try to get data from the Clipboard. Since Clipboard.GetImage() is executed right after just the start of SnippingTool, you haven't been able to assign an image to the Clipboard in time. (because you just got a few milliseconds to do so)

So just access the Clipboard after you are 100% sure that you just stored your desired image in it. Do it e.g. in a button click event:

private void button1_Click(object sender, EventArgs e)
{
    IDataObject obj = Clipboard.GetDataObject();
    Image img1 = Clipboard.GetImage();

    //now do with the image whatever you want, you may want to store it as a public field thought to
    //access the image in your whole application
}

Or you can monitor the Clipboard with some p/invoke. Here is a guide how to get notified once the value of the Clipboard has changed. Basically you set a viewer to the Clipboard, so you recieve a WindowsMessage about a value being assigned to the Clipboard which can be intercept in the WndProc() function by overriding it and adding a check for this specific mentioned WindowsMessage.

L. Guthardt
  • 1,990
  • 6
  • 22
  • 44
  • For how long shall i wait to access the image after capturing the image? Because when i try to paste image in paint it works fine. – RoshanJha Jul 17 '18 at 11:25
  • @RoshanJha You have to capture the image **and** store it in the `Clipboard`. After that you can instant access your `Clipboard` and will get the image. – L. Guthardt Jul 17 '18 at 11:27
  • so basically you are saying after capturing the image i have to set the Clipboard image after that only i can retrieve it? – RoshanJha Jul 17 '18 at 11:33
  • @RoshanJha If I understood your english correct, then yes. `1.`Make a screenshot with SnippingTool. `2.`Get this screenshot any way you want into the Clipboard. It's your decision how you give the image to the Clipboard. `3.`Since you send the image to the Clipboard you can access the image through the Clipboard. I don't know if you understood yet, but the Clipboard is the temporary storage for *Copy and Paste*. So `Ctrl + C`. The SnippingTool does not, according to my knowledge, auto pastes the screenshot to the Clipboard. That's what you have to do, to be able to access it later again. – L. Guthardt Jul 17 '18 at 11:37
  • 1
    @RoshanJha If it doesn't work please let me know. If it works though, feel free to accept my answer as the accepted answer. :) – L. Guthardt Jul 17 '18 at 13:35
  • @RoshanJha What about any reaction? Please tell me if you understood my solution and if it works for you. – L. Guthardt Jul 25 '18 at 06:38
  • Hey, Thank for concern. That didn't work out. It was running perfect on my `visual studio` but when hosted on `IIS`, `SnippingTool` was not firing up. I went with some workaround solution in the end. And i don't know if this makes any difference or not but I am working on an `MVC` application not `Web Forms` or `Window Application`. – RoshanJha Jul 27 '18 at 16:47