none
How to take screen shot of desktop via remote RRS feed

  • Question

  • Hello,

    I have a WCF service hosted in windows service which is installed on a computer running in window 7. I Plan to take the screen shot of the system with help of the WCF service and system Ip address. So first i tried writing the screenshot code in WCF service, but it threw an error The handle is invalid at System.Drawing.Graphics.CopyFromScreen. Then i created a windows form application and put the same code on that and called this windows service from WCF using Process.Start.This time also it threw the same error,but this code work if i run the windows app form manually.So can any one help me on this?

    private void CaptureScreen()
            {
                try
                {
                    string directorypath = string.Empty;

                    var screens = Screen.AllScreens;
                    string path = string.Empty;
                    foreach (var screen in screens)
                    {
                        
                        using (Bitmap bmpScreenCapture = new Bitmap(screen.Bounds.Width, screen.Bounds.Height))
                        {
                            using (Graphics g = Graphics.FromImage(bmpScreenCapture))
                            {
                                g.CopyFromScreen(screen.Bounds.X,
                                                 screen.Bounds.Y,
                                                 0, 0,
                                                 bmpScreenCapture.Size,
                                                 CopyPixelOperation.SourceCopy);
                                g.Dispose();
                            }

                            if (IsDriveExists(@"D:\"))
                            {
                                directorypath = string.Format(@"D:\QTA\Screenshot");
                            }
                            else
                            {
                                directorypath = string.Format(@"C:\QTA\Screenshot");
                            }

                            if (!Directory.Exists(directorypath))
                            {
                                Directory.CreateDirectory(directorypath);
                            }


                            ImageCodecInfo myImageCodecInfo;
                            Encoder myEncoder;
                            EncoderParameter myEncoderParameter;
                            EncoderParameters myEncoderParameters;

                            // Get an ImageCodecInfo object that represents the PNG codec.
                            myImageCodecInfo = GetEncoderInfo("image/png");

                            // Create an Encoder object based on the GUID

                            // for the Quality parameter category.
                            myEncoder = Encoder.Quality;

                            // Create an EncoderParameters object.

                            // An EncoderParameters object has an array of EncoderParameter

                            // objects. In this case, there is only one

                            // EncoderParameter object in the array.
                            myEncoderParameters = new EncoderParameters(1);

                            // Save the bitmap as a JPEG file with quality level 25.
                            myEncoderParameter = new EncoderParameter(myEncoder, 25L);
                            myEncoderParameters.Param[0] = myEncoderParameter;

                            //  bmpScreenCapture.Save(@"D:\QTA\Screenshot\screenshot.png", myImageCodecInfo, myEncoderParameters);
                            // var path = Path.Combine(directorypath, $"{screenName}_screenshot.png");
                            //bmpScreenCapture.Save(@"D:\QTA\Screenshot\primary_screenshot.png", myImageCodecInfo, myEncoderParameters);
                            if (screen == Screen.PrimaryScreen)
                            {
                                bmpScreenCapture.Save(@"D:\QTA\Screenshot\primaryscreenshot.png", myImageCodecInfo, myEncoderParameters);
                            }
                            else
                            {
                                bmpScreenCapture.Save(@"D:\QTA\Screenshot\secondaryscreenshot.png", myImageCodecInfo, myEncoderParameters);
                            }
                            bmpScreenCapture.Dispose();
                        }
                    }
                }
                catch(Exception ex)
                {
                    Log(ex.ToString());
                }
            }


    Saturday, October 7, 2017 12:38 PM

All replies

  • Hi,

    For this you need to configure a Mqtt server, use "io.adafruit.com" , your application runs on computer will work as a client you can send command to client pc remotely to capture screenshot and receive screenshot via email. All process requires internet connectivity.

    If you found my answer helpful please give Vote as helpful

    Saturday, October 7, 2017 1:29 PM
  • https://www.codeproject.com/Articles/515253/Service-Oriented-Architecture-and-WCF

    I think  you have a misconception of what WCF is for on the when, what and why.

    Saturday, October 7, 2017 4:43 PM
  • Windows services run in session 0, a non-interactive session. When users log onto windows their desktop is created in a new, interactive session.  The service in session 0 is isolated from users' interactive sessions.  In order to obtain a screenshot of a user's desktop in their interactive session you will need to have your windows service use a helper process that runs in the interactive user's session to obtain the screenshot and then transfer the image to the service using one of the various interprocess communications methods such as named pipes, COM, etc.
    Saturday, October 7, 2017 7:49 PM
  • Hi VishnuOPR,

    Thank you for posting here.

    According to your question is more related to WCF, I will move it to Windows Communication Foundation, Serialization, and Networking forum for suitable support.

    This forum discusses and ask the C# programming language, IDE, libraries, samples and tools. If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 9, 2017 6:21 AM
  • Hi VishnuQPR,

    I agree with DA924x and RLWA32, and it is impossible to take screen shot of desktop by using service.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 10, 2017 2:25 AM