Screen capture using windows service

扶醉桌前 提交于 2019-12-01 21:40:41

问题


Even there are lot of questions regarding this issue i couldn't find proper solution for this. I'm creating windows service to capture screen(windows 7). ( i tried this using windows application and it works properly. )

When I'm going to start the service then it says i cant start the service. When i check the windows log it mentioned following error.

Service cannot be started. System.ComponentModel.Win32Exception (0x80004005): The handle is invalid
   at System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize, CopyPixelOperation copyPixelOperation)
   at ScreenCaptureService.ScreenCaptureService.TraceService() in d:\SourceControl\Test\Test\ScreenCapture\ScreenCaptureService\ScreenCaptureService.cs:line 63
   at ScreenCaptureService.ScreenCaptureService.OnStart(String[] args) in d:\SourceControl\Test\Test\ScreenCapture\ScreenCaptureService\ScreenCaptureService.cs:line 32
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

My code as below:

public partial class ScreenCaptureService : ServiceBase
{

    private static Bitmap bmpScreenshot;
    private static Graphics gfxScreenshot;
    System.Timers.Timer timer = new System.Timers.Timer();
    public ScreenCaptureService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {

        TraceService();
        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
        timer.Interval = 300000;          
        timer.Enabled = true;
    }

    protected override void OnStop()
    {
        timer.Enabled = false;
        TraceService();
    }

    private void TraceService()
    {
        string fileName = "D:\\Screen\\abc.png";
        bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);


        gfxScreenshot = Graphics.FromImage(bmpScreenshot);            
        gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

        bmpScreenshot.Save(fileName, ImageFormat.Png);
    }

    private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
        TraceService();
    }
}

What i have missed in here..

EDIT : when i tick on allow service to interact with desktop then it shows following error in log.

Service cannot be started. System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.
   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
   at ScreenCaptureService.ScreenCaptureService.TraceService() in d:\SourceControl\Test\Test\ScreenCapture\ScreenCaptureService\ScreenCaptureService.cs:line 66
   at ScreenCaptureService.ScreenCaptureService.OnStart(String[] args) in d:\SourceControl\Test\Test\ScreenCapture\ScreenCaptureService\ScreenCaptureService.cs:line 32
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

回答1:


I solved my question as mentioned in Here, but then i faced another problem. It saves as black window due to mentioned reason in the above link. so finally i changed my application in to windows form application



来源:https://stackoverflow.com/questions/18870987/screen-capture-using-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!