我知道這是一個關於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?
請顯示您的代碼,並解釋您卡住的部分代碼。 – mbeckish 2013-03-12 20:13:22
@mbeckish,在這裏) – Overrided 2013-03-12 20:31:31
@mbeckish - 不,因爲我不需要在會話1中從會話0開始控制檯應用程序...我需要從會話1到會話0獲取gui信息並將其保存爲圖像。 – Overrided 2013-03-12 20:47:11