1

First all, I need appreciate Mr. "eddie_cat" for provide effective response on my question. He own the credit of "ScreenCapture" method. Though this method not solve my issue (in windows service side it is not working), but I believe the code can be used in normal windows form for screen capture.

Thought I can not find a solution to solve the issue, I find a good suggestion from Mr. " Davide Piras" Windows service couldnt get screenshot in windows 7

His Post said:

"a windows service is designed to run also when there are no users connected, it works like a server process always up and listening, or up and doing something, or idle.

I think what you need is a client application which runs inside every logged user' session and eventually does the job then, if needed, communicates with the service to carry some job done.

I am saying here that instead of having the windows service running in another session than 0 you can create a small executable (probably with no UI at all) that starts up from the start up folder of all users at every user login. such application is then running inside the proper session and has access to it, it can get the screenshot then either store it somewhere itself or call some end points in your Windows Service (running always in session 0) and make the service to elaborate the screenshot taken from the client application of it.

this is the way I would do it, not trying some "magic" to tell Vista and 7 to start a service inside a session of a user that in the end is not logged in yet when the system starts."

Community
  • 1
  • 1
Deep in Development
  • 497
  • 2
  • 8
  • 24
  • Actually, I just need simply way to capture whole screen then save it to a file. Any simple solution are very welcome. – Deep in Development Sep 30 '14 at 18:33
  • @Marc B, Grant Winney. My question is fifference with others. My issue cause by try to capture screen in a windows server where the server capture the session screen not the user active window session. – Deep in Development Sep 30 '14 at 20:11
  • Still a duplicate, but not of the listed question. Please change it to this one http://stackoverflow.com/questions/18891819/windows-service-screen-capture-returns-black-screen?lq=1 – eddie_cat Sep 30 '14 at 20:11
  • @Marc B, Grant Winney. I did a lot of research on stack overflow. The question seems duplicate with others but all of these question does not has a detail code level answer. So I would like address my question again here and expect a code level help. So please remove the [duplicate] from my question title. – Deep in Development Oct 01 '14 at 14:02
  • you might be better off asking a new question which details your current predicament as you understand it now. Your first question is very different from what you are asking now & as you can't delete this one & it's not likely to get anymore attention it would probably be more useful to post a new one explaining why that one is not a duplicate of the others you've looked at – eddie_cat Oct 01 '14 at 14:07
  • 1
    @eddie_cat. Thanks! I just read some post from "Davide Piras"http://stackoverflow.com/questions/7454106/windows-service-couldnt-get-screenshot-in-windows-7. He said:"I am saying here that instead of having the windows service running in another session than 0 you can create a small executable (probably with no UI at all) that starts up from the start up folder of all users at every user login. such application is then running inside the proper session and has access to it, it can get the screenshot then either store it somewhere itself or call some end points in your Windows Service..." – Deep in Development Oct 01 '14 at 14:27
  • We had similar issues as well, but we solved them: https://stackoverflow.com/a/45095509/125406 – Michael Silver Jul 14 '17 at 05:53

1 Answers1

6

Use this instead, much simpler:

Rectangle bounds = Screen.PrimaryScreen.Bounds;
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
   graphics.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size);
}
bitmap.Save("C:\\temp\\test.png", ImageFormat.Png);

The code you copied from that other question was meant to solve a very specific problem. You don't need to do all that just to capture a screenshot. :)

eddie_cat
  • 2,527
  • 4
  • 25
  • 43
  • Hi eddie_cat, thanks for your helping. How to change your code to save it to a specific folder like @"c:\temp\test.png". I try use bitmap.Save(@"c:\temp\test.png"); instead yours but not working. – Deep in Development Sep 30 '14 at 18:47
  • Sorry @Matthew I copied this code from a project of mine where I was saving to a `MemoryStream`. I will update with saving to a file. – eddie_cat Sep 30 '14 at 18:49
  • I use code bitmap.Save(@"c:\temp\test.png", ImageFormat.Png); but it still not save to a file. – Deep in Development Sep 30 '14 at 18:56
  • Is there an error message? – eddie_cat Sep 30 '14 at 18:56
  • There is no error message. just no file to save. – Deep in Development Sep 30 '14 at 18:57
  • @Matthew Go to `My Computer`, click on `C:`, and then click on `temp`. (Are you sure this is where you meant to save it?) – ispiro Sep 30 '14 at 18:59
  • I double check "C:\temp" there is nothing there. – Deep in Development Sep 30 '14 at 19:04
  • Are you sure this code is being executed? – eddie_cat Sep 30 '14 at 19:06
  • @Matthew I think there's actually a mistake in the code. It should be `graphics.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size);`. Or perhaps use: `Rectangle bounds = Screen.GetBounds(Point.Empty);`. – ispiro Sep 30 '14 at 19:06
  • @ispiro it's not a bug, run it in a console app – eddie_cat Sep 30 '14 at 19:07
  • I guarantee the code is executed. – Deep in Development Sep 30 '14 at 19:07
  • It runs fine because they all happen to be 0. But what if the OP has multiple screens that somehow mess that up? – ispiro Sep 30 '14 at 19:08
  • @ispiro then he should have mentioned that in his post. In any case if that is the issue I will update the answer :) – eddie_cat Sep 30 '14 at 19:10
  • @eddie_cat I'm not the downvoter, if that's what you mean. Just pointing it out. (Check out Intellisense for `CopyFromScreen`.) – ispiro Sep 30 '14 at 19:11
  • @ispiro nah I didn't mean that. If it is a bug I'd want to know, I just use this code often and it always works for me so I was surprised to hear that. But I do only use it on one screen & it makes sense to change it – eddie_cat Sep 30 '14 at 19:12
  • 1
    @Matthew how are you verifying that the code is executed? Have you stepped through in the debugger to make sure it's getting there? – eddie_cat Sep 30 '14 at 19:14
  • @eddie_cat, the code run without any errors, but there is nothing in the c:\temp\ folder. – Deep in Development Sep 30 '14 at 19:14
  • @Matthew that doesn't mean that *this* bit of code is being run. You could have a problem somewhere else entirely that's preventing this from ever being executed. – eddie_cat Sep 30 '14 at 19:14
  • @Matthew I also noticed you tagged this WinForms but it looks to be a Windows Service application. Is it supposed to be? I don't see where you are ever calling the ScreenCapture() method either – eddie_cat Sep 30 '14 at 19:18
  • @eddie_cat With the code I used before, it create a dark (black) file in the c:\temp folder. I just replace them with yours. But unfortunately, there is no file created in that folder. – Deep in Development Sep 30 '14 at 19:19
  • @Matthew see this question http://stackoverflow.com/questions/18891819/windows-service-screen-capture-returns-black-screen – eddie_cat Sep 30 '14 at 19:22
  • @eddie_cat please see updated post which shows full service code. – Deep in Development Sep 30 '14 at 19:23
  • @Matthew the reason you are getting nothing now and were getting a black screen before is that your Windows Service does not know what to take a screenshot of. Please read the linked question and related to implement a fix. The issue is not the screenshot code itself. – eddie_cat Sep 30 '14 at 19:24
  • @eddie_cat. Thanks for your effort. I believe your code is correct. I read the link your provide, now I know the issue is "It captured the service session not the active user session screen." but I still can not solve the issue. If you could help modify my code and make it work that will be great. Anyway Many Thanks!! – Deep in Development Sep 30 '14 at 20:07
  • @Matthew I unfortunately don't know how to solve your issue, but I found this really great article linked in another question about sessions in Windows which I think will put you on the right track. Check it out. http://www.brianbondy.com/blog/id/100/ – eddie_cat Sep 30 '14 at 20:10