1

I have a c# code for take screenshot from desktop

//Create a new bitmap.
            var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                           Screen.PrimaryScreen.Bounds.Height,
                                           PixelFormat.Format32bppArgb);

            // Create a graphics object from the bitmap.
            var gfxScreenshot = Graphics.FromImage(bmpScreenshot);

            // Take the screenshot from the upper left corner to the right bottom corner.
            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                        Screen.PrimaryScreen.Bounds.Y,
                                        0,
                                        0,
                                        Screen.PrimaryScreen.Bounds.Size,
                                        CopyPixelOperation.SourceCopy);

            // Save the screenshot to the specified path that the user has chosen.
            bmpScreenshot.Save("Screenshot.png", ImageFormat.Png);

this code work fine, but when have a problem when running a program that need "administrator privilege" and show this image:

image

the above code dont work and throw an exception : "The handle is invalid"

how can i solve this problem?

MrMining
  • 11
  • 2
  • You have the same problem as in the marked duplicate, with the same answer: "you can't". The UAC prompt is running in a secured context, which precludes your code from having access to the bits for a screenshot. All you can do is turn the feature off (not good for keeping your computer safe, but you can do it if you want to). – Peter Duniho Jun 20 '17 at 05:17
  • The TeamViewer has been able to fix this. I wanted to know how it could take a screenshot from the secure desktop. – MrMining Jun 21 '17 at 04:20
  • Talk to TeamViewer. Or better yet, just use TeamViewer to get your screenshot, since they already (apparently) have working code. The only way I see that could work is to insert a video driver or similar, so that the code is still involved. You won't be able to accomplish this using C#/.NET. Note that virtualizing the video driver is a routine task for something like TeamViewer, since it's already intercepting all graphics to transmit to a remote computer. You'll have to write code that is similar to that. – Peter Duniho Jun 21 '17 at 04:25

0 Answers0