4

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)
DevT
  • 4,843
  • 16
  • 59
  • 92
  • 1
    I'd be VERY careful doing something like this, CopyFromScreen has had a memory leak in it forever that causes applications that take screenshots in this manner to cause some serious issues. – Ashigore Sep 18 '13 at 11:30
  • If you're running this as a service. exactly what screen do you expect to be taking a screen shot of. What if multiple users are logged. What if no-one is logged in. What if the computer is locked. Basically, that error is telling you that from the context of a windows service, the service doesn't have a valid handle to a screen from which it can take a copy/screenshot. – Eoin Campbell Sep 18 '13 at 11:33
  • @Ashigore do u have any idea to do this in another way? (it should run in background and user cant see it) – DevT Sep 18 '13 at 11:34
  • On the properties of your service, is the `interact with desktop` checked? – Jeroen van Langen Sep 18 '13 at 11:34
  • @EoinCampbell i need to get only active window. – DevT Sep 18 '13 at 11:35
  • @DevT It should be possible to use a task schedule or services to periodically execute a process to take a screenshot that then exits. – Ashigore Sep 18 '13 at 11:35
  • @JeroenvanLangen - yes its checked – DevT Sep 18 '13 at 11:35
  • @Ashigore - any examples? – DevT Sep 18 '13 at 11:36
  • @Ashigore can give us more info about the issue with `CopyFromScreen`, thanks. – Alessandro D'Andria Sep 18 '13 at 11:37
  • @AlessandroD'Andria This is what I could find with a quick google. http://social.msdn.microsoft.com/Forums/vstudio/en-US/cedaa0f2-383c-4e61-92c0-c09123cb67cd/need-help-confirming-possible-bug-in-graphicscopyfromscreen-regarding-leakage-of-gdi-object-handle I've confirmed this leak still existed in .net 4.0 in the past when I;ve tried to do exactly this sort of thing. I'm not sure if it's fixed in 4.5. – Ashigore Sep 18 '13 at 11:41
  • @DevT take a look at [this](http://stackoverflow.com/questions/18870574/screencapture-program-taking-screenshot/18870833#18870833) i've posted code to capture active windows (with the leaked `CopyFromScreen` :). – Alessandro D'Andria Sep 18 '13 at 11:45
  • @AlessandroD'Andria i tried ur answer. but it also not working for me. :( – DevT Sep 18 '13 at 12:02
  • 1
    I solved this problem. http://stackoverflow.com/questions/18891819/windows-service-screen-capture-returns-black-screen but now it saves as black window due to mentioned reason in the above link. so finally i changed my application in to windows form application – DevT Oct 05 '13 at 05:30
  • @DevT may be it's worth posting your last comment as answer with some notes so people could easily see the possible solution. – Fedor Dec 13 '13 at 13:16
  • @Fyodor - thanks for ur suggestion. so i added my final conclusion as a answer. :) – DevT Dec 13 '13 at 16:22

1 Answers1

2

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

Community
  • 1
  • 1
DevT
  • 4,843
  • 16
  • 59
  • 92