c# - Taking screenshots with Windows Service in Windows 7 -
this question has answer here:
i know it's old question screenshots in win 7 winservice on c#. have read articles on stack overflow , lot on codeproject...i know 0 session services , starting win vista & allow service interact desktop checking... i'm stuck (i can't take screenshot service, because don't know display image(screen)) saved.
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(); } } }
this mytimer class:
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); } } }
and code returns blind (empty) screenshots... works , service can`t screenshot because in 0 session...how gui session of current logined user?
since need access desktops other session 0, consider using task in task scheduler instead of windows service capture screenshots. ts makes starting process within user's session easier. task should configured launched when user logs in. you'll need set user account task runs group, such users
, , have task run when user logged on. these 2 security options essential creating process access user's desktop session. i've been able in past using user32 / gdi32 api calls taking screenshots.
if still want use windows service aggregate screenshots somehow, use wcf send screenshots task processes windows service.
Comments
Post a Comment