Capture screen on server desktop session

后端 未结 6 1375
臣服心动
臣服心动 2020-11-30 06:17

I have developed a GUI test framework that does integrationtesting of our company website on a scheduled basis. When something fails, it\'ll take a screenshot of the desktop

6条回答
  •  不知归路
    2020-11-30 06:36

    What I did to solve this is call tscon.exe and tell it to redirect the session back to the console just before the screenshot is taken. It goes like this (note, this exact code is untested):

    public static void TakeScreenshot(string path) {
        try {
            InternalTakeScreenshot(path);
        } catch(Win32Exception) {
            var winDir = System.Environment.GetEnvironmentVariable("WINDIR");
            Process.Start(
                Path.Combine(winDir, "system32", "tscon.exe"),
                String.Format("{0} /dest:console", GetTerminalServicesSessionId()))
            .WaitForExit();
    
            InternalTakeScreenshot(path);
        }
    }
    
    static void InternalTakeScreenshot(string path) {
        var point = new System.Drawing.Point(0,0);
        var bounds = System.Windows.Forms.Screen.GetBounds(point);
    
        var size = new System.Drawing.Size(bounds.Width, bounds.Height);
        var screenshot = new System.Drawing.Bitmap(bounds.Width, bounds.Height);
        var g = System.Drawing.Graphics.FromImage(screenshot)
        g.CopyFromScreen(0,0,0,0,size);
    
        screenshot.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); 
    }
    
    [DllImport("kernel32.dll")]
    static extern bool ProcessIdToSessionId(uint dwProcessId, out uint pSessionId);
    
    static uint GetTerminalServicesSessionId()
    {
        var proc = Process.GetCurrentProcess();
        var pid = proc.Id;
    
        var sessionId = 0U;
        if(ProcessIdToSessionId((uint)pid, out sessionId))
            return sessionId;
        return 1U; // fallback, the console session is session 1
    }
    

提交回复
热议问题