使用Windows服务的屏幕截图 [英] Screen capture using windows service

查看:109
本文介绍了使用Windows服务的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个问题,即使有很多问题,我也找不到合适的解决方案.我正在创建Windows服务以捕获屏幕(Windows 7).(我使用Windows应用程序尝试过,它可以正常工作.)

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. )

当我要启动该服务时,它说我无法启动该服务.当我检查Windows日志时,它提到了以下错误.

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)

我的代码如下:

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();
    }
}

我在这里错过了什么.

:当我在允许服务与桌面交互上打钩时,它会在日志中显示以下错误.

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)

推荐答案

我解决了

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

这篇关于使用Windows服务的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆