在 Windows 7 中使用 Windows 服务截取屏幕截图 [英] Taking screenshots with Windows Service in Windows 7

查看:146
本文介绍了在 Windows 7 中使用 Windows 服务截取屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个关于在 c# 上使用 winService 的 Win 7 屏幕截图的老问题.我在 Stack Overflow 上阅读了所有关于此的文章,在 CodeProject 上阅读了很多……我知道服务的 0 会话,从 Win Vista & 开始.关于允许服务与桌面检查交互...但是我卡住了(我无法从服务中截取屏幕截图,因为我不知道显示图像(屏幕)的保存位置.

I know it's an old question about screenshots in Win 7 with winService on c#. I have read all articles about this on Stack Overflow and a lot on CodeProject...I know about 0 session for services , starting from Win Vista & about Allow service to interact with desktop checking... But I'm stuck (I can't take screenshot from service, because I don't know where display image(screen)) is saved.

   using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Data;
   using System.Diagnostics;
   using System.Linq;
   using System.ServiceProcess;
   using System.Text;
   using System.Timers;

   namespace MyThirdTry
   {
public partial class Third : ServiceBase
{
    private static MyTimer aTimer;
    public Third()
    {
        InitializeComponent();
        if (!System.Diagnostics.EventLog.SourceExists("MySource"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "MySource", "MyNewLog");
        }
        eventLog1.Source = "MySource";
        eventLog1.Log = "MyNewLog";
        aTimer = new MyTimer();
        aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
        aTimer.Interval = 10000;
    }

    protected override void OnStart(string[] args)
    {
        eventLog1.WriteEntry("Started\t" + DateTime.Now.ToString("G"));
        aTimer.Enabled = true;
    }

    protected override void OnStop()
    {
        aTimer.Enabled = false;
        eventLog1.WriteEntry("Stopped\t" + DateTime.Now.ToString("G"));
    }
    protected override void OnPause()
    {
        aTimer.Enabled = false;
        eventLog1.WriteEntry("Paused\t" + DateTime.Now.ToString("G"));
        base.OnPause();
    }
    protected override void OnContinue()
    {
        base.OnContinue();
        aTimer.Enabled = true;
        eventLog1.WriteEntry("Continued\t" + DateTime.Now.ToString("G"));
    }
    protected override void OnShutdown()
    {
        aTimer.Enabled = false;
        eventLog1.WriteEntry("Shutted down\t" + DateTime.Now.ToString("G"));
        base.OnShutdown();
    }
    private void aTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        eventLog1.WriteEntry("Evented\t" + aTimer.TimeTaker() + "\t" + Environment.CurrentDirectory);
        aTimer.TakeScreenShot();
    }
}
}

这是 MyTimer 类:

This is MyTimer class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Timers;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Security.Principal;
    using System.IO;

    namespace MyThirdTry
    {
class MyTimer:Timer
{
    Bitmap mainBit;
    System.Windows.Forms.Screen main;

    public string TimeTaker()
    {
        return DateTime.Now.ToString("G");
    }

    public void TakeScreenShot()
    {
        string PathToSave = @"c:\results";
        System.IO.Directory.CreateDirectory(PathToSave);

        main = System.Windows.Forms.Screen.PrimaryScreen;
        mainBit = new Bitmap(main.Bounds.Width, main.Bounds.Height, PixelFormat.Format32bppArgb);
        Graphics gScreenShot = Graphics.FromImage(mainBit);

        gScreenShot.CopyFromScreen(main.Bounds.X,
                                   main.Bounds.Y,
                                   0, 0,
                                   main.Bounds.Size,
                                   CopyPixelOperation.SourceCopy);

        string fileName = "result" + Directory.GetFiles(PathToSave).Count().ToString().Trim() + ".png";
        mainBit.Save(System.IO.Path.Combine(PathToSave, fileName), System.Drawing.Imaging.ImageFormat.Png);
    }
}
}

此代码返回盲(空)屏幕截图...一切正常,但服务无法获取屏幕截图,因为它处于 0 会话中...如何从当前登录用户的会话中获取 gui?

And this code returns blind (empty) screenshots... All works , but service can`t get screenshot because it is in 0 session...how to get gui from session of current logined user?

推荐答案

由于您需要访问会话 0 以外的桌面,请考虑使用任务计划程序中的任务而不是 Windows 服务来捕获屏幕截图.TS 使在用户会话中启动进程变得更加容易.该任务应配置为在任何用户登录时启动.您需要将任务运行的用户帐户设置为一个组,例如 Users,并且仅在以下情况下运行该任务用户已登录.这两个安全选项对于创建访问用户桌面会话的进程至关重要.过去,我使用 User32/Gdi32 API 调用截取屏幕截图成功地做到了这一点.

Since you need to access desktops other than session 0, consider using a task in Task Scheduler instead of a Windows service to capture the screenshots. TS makes starting a process within the user's session much easier. The task should be configured to be launched when any user logs in. You'll need to set the user account that the task runs as to a group, such as Users, and have the task run only when the user is logged on. These two security options are essential for creating the process with access to the user's desktop session. I've been able to successfully do this in the past using the User32 / Gdi32 API calls for taking screenshots.

如果您仍然想使用 Windows 服务以某种方式聚合屏幕截图,则使用 WCF 将屏幕截图从任务进程发送到 Windows 服务.

If you still want to use a Windows service to aggregate the screenshots somehow, then use WCF to send the screenshots from the task processes to the Windows service.

这篇关于在 Windows 7 中使用 Windows 服务截取屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆