0

I have a problem with the screenshot function in my console app written in C#. In debug mode I get a picture saved that represents the screen. But in service mode only a black screen. I have a hunch that it could be related to the session. Is there a way to modify this so that I don't get a black image in service mode but the screen? I want to capture all screens from the PC running the service, of the active user.

internal class ScreenCapture
    {
       
        /// Creates an Image object containing a screen shot of the entire desktop
        public Image CaptureScreen()
        {
            return CaptureWindow(User32.GetDesktopWindow());
        }
      
        /// Creates an Image object containing a screen shot of a specific window
        public Image CaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            // create a device context we can copy to
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
            // restore selection
            GDI32.SelectObject(hdcDest, hOld);
            // clean up
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);
            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);
            return img;
        }
        /// <summary>
        /// Captures a screen shot of a specific window, and saves it to a file
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
        {
            Image img = CaptureWindow(handle);
            img.Save(filename, format);
        }
        /// <summary>
        /// Captures a screen shot of the entire desktop, and saves it to a file
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        public void CaptureScreenToFile(string filename, ImageFormat format)
        {
            Image img = CaptureScreen();
            img.Save(filename, format);
        }

        /// <summary>
        /// Helper class containing Gdi32 API functions
        /// </summary>
        private class GDI32
        {

            public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
            [DllImport("gdi32.dll")]
            public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
                int nWidth, int nHeight, IntPtr hObjectSource,
                int nXSrc, int nYSrc, int dwRop);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
                int nHeight);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteObject(IntPtr hObject);
            [DllImport("gdi32.dll")]
            public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
        }

        /// <summary>
        /// Helper class containing User32 API functions
        /// </summary>
        private class User32
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }
            [DllImport("user32.dll")]
            public static extern IntPtr GetDesktopWindow();
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowDC(IntPtr hWnd);
            [DllImport("user32.dll")]
            public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
        }
    }

 // Screenshot function, capture entire screen, and save it to a file
ScreenCapture sc = new ScreenCapture();
Image img = sc.CaptureScreen();
string pathOfScreenshot = "C:\\ProgramData\\war\\screenshots";
if (!Directory.Exists(pathOfScreenshot)) {
   Directory.CreateDirectory(pathOfScreenshot);
}
var date = DateTime.UtcNow;
string image = pathOfScreenshot + "\\screenshot_" + date.Day + date.Month + date.Year + "_" + date.Hour + date.Minute + date.Second + ".jpeg";
img.Save(image, ImageFormat.Jpeg);

I tried to start the whole thing as a new process that calls the exe file externally, so the screenshot is its own exe file, but without success.

Marko
  • 16
  • 3
  • I wrote a quick console app that calls `sc.CaptureScreenToFile(@"c:\temp\sc.bmp", ImageFormat.Bmp);`, then ran the compiled .exe from a separate command window and it worked fine. – Rufus L Dec 02 '22 at 08:59
  • I copied your code above and added it as a class to a console project that has literally one line of code in the `Main` method: `new ScreenCapture().CaptureScreenToFile(@"c:\temp\sc.bmp", ImageFormat.Bmp);` – Rufus L Dec 02 '22 at 09:06
  • It works in debug mode but not in service mode, still get a black screen in service mode (probably because it is screenshotting the wrong session) – Marko Dec 02 '22 at 09:14
  • 1
    The whole point of a service is that it runs without a GUI. What would you like it to capture? – Palle Due Dec 02 '22 at 09:25
  • I want to capture all screens from the PC running the service, of the active user. – Marko Dec 02 '22 at 09:28
  • Oh, I misunderstood your distinction between debug and service mode. Try creating process as user: https://stackoverflow.com/questions/4147821/start-a-windows-service-and-launch-cmd/4147868#4147868 – Rufus L Dec 03 '22 at 14:58

0 Answers0