2013-03-12 119 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服務。