2009-12-02 17 views
4

매 초마다 바탕 화면 캡쳐 화면을 캡처해야합니다. Winform 응용 프로그램에서 잘 실행됩니다. 그러나 코드를 Windows 서비스로 옮긴 후에는 스크린 샷을 캡처하지 않습니다. 왜 그렇게하지 않는지? 여기C# : Windows 서비스에서 캡처 화면

는 (서비스 속성에서) 확인 "서비스와 데스크톱 상호 작용 허용"한 마 코드

public partial class ScreenCaptureService : ServiceBase 
    { 
     System.Timers.Timer timer = new System.Timers.Timer(); 

     public ScreenCaptureService() 
     { 
      InitializeComponent();    
      this.timer.Interval = 1000; 
      this.timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); 

     } 

     void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
     { 
      CaptureScreen(); 
     } 

     protected override void OnStart(string[] args) 
     { 
      if (!EventLog.SourceExists(this.ServiceName, Environment.MachineName)) 
      { 
       EventLog.CreateEventSource(
        new EventSourceCreationData(
         this.ServiceName, 
         Environment.MachineName 
         ) 
       ); 
      } 

      EventLog.WriteEntry(this.ServiceName, "The OnStart event has been called"); 
      this.timer.Enabled = true; 
      CaptureScreen(); 
     } 

     protected override void OnStop() 
     { 
      EventLog.WriteEntry(this.ServiceName, "The OnStop event has been called"); 
      this.timer.Enabled = false; 
     } 

     static int count = 1; 
     private void CaptureScreen() 
     { 

      Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); 

      Graphics graphics = Graphics.FromImage(printscreen as Image); 

      graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size); 

      printscreen.Save(@"C:\printscreen" + count++ + ".jpg", ImageFormat.Jpeg); 

      EventLog.WriteEntry(this.ServiceName, "Screenshot Captured"); 
     } 
} 
+0

도 참조하십시오. http://stackoverflow.com/questions/1002064/screen-capture-from-windows-service – rogerdpack

+0

참조 http://stackoverflow.com/questions/5200341/capture-screen-on-server-desktop -session/12851218 – Theraot

답변

8

입니까?

+2

windows xp 나는 이것이 해결책이라고 알고 있지만 윈도우 비자/7과 함께 작동하는지 확실하지 않다. – Peter

+0

감사합니다. 이 옵션은 선택하지 않았습니다. 코드를 사용하여 어떻게 확인할 수 있습니까? – Mohsan

+3

Petoj가 정확합니다. 이것은 XP에서 작동하지만 Vista/7에서는이를 허용하지 않습니다. – MutantNinjaCodeMonkey