2013-03-12 79 views
2

我知道这是一个关于Win 7中winuser的屏幕截图的老问题。我已经阅读了关于Stack Overflow的所有文章以及CodeProject上的很多...我知道关于服务的0会话,从Win Vista &开始关于允许服务与桌面检查进行交互... 但是我被卡住了(我无法从服务中截图,因为我不知道显示图像(屏幕)在哪里保存。在Windows 7中使用Windows Service获取屏幕截图

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类:

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?

+0

请显示您的代码,并解释您卡住的部分代码。 – mbeckish 2013-03-12 20:13:22

+0

@mbeckish,在这里) – Overrided 2013-03-12 20:31:31

+0

@mbeckish - 不,因为我不需要在会话1中从会话0开始控制台应用程序...我需要从会话1到会话0获取gui信息并将其保存为图像。 – Overrided 2013-03-12 20:47:11

回答

1

由于您需要访问除会话0以外的桌面,因此请考虑在任务计划程序中使用任务而不是Windows服务来捕获屏幕截图。 TS使得在用户会话中启动一个流程变得更容易。该任务应配置为在任何用户登录时启动。您需要设置任务运行的用户帐户(例如Users),并且只有在用户登录后才能运行该任务。这两个安全选项对创建访问用户桌面会话的过程至关重要。过去我已经能够成功地使用User32/Gdi32 API调用进行屏幕截图。

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

相关问题