2

I've read tons of other questions and googled the issue, but I can only figure out how to do it if I'm using winforms. I'm currently writing a library, and one of the functions of the library is to handle logging. One of the features I'm implementing for it is to automatically take a screenshot of the page before writing the issue to the log. The issue with this is that I don't know which monitor to take a screenshot of, so if the user moves the browser to a different monitor, I still take a shot of the Primary one.

public static Bitmap ScreenShot(string saveLocation, string fileName)
{
    Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    Graphics.FromImage(bitmap).CopyFromScreen(0, 0, 0, 0, bitmap.Size);
    string savePath = Path.Combine(Path.GetDirectoryName(saveLocation), "ScreenShots");
    if (!Directory.Exists(savePath))
        Directory.CreateDirectory(savePath);
    bitmap.Save(Path.Combine(savePath, fileName), ImageFormat.Png);
    return bitmap;
}

I've tried Screen.FromControl(), but since it's not a winforms application, I don't have any System.Windows.Forms.Controls for it to find.

Does ASP.NET have any equivalent? Or something I can explicitly cast to a winform control?

Sivvy
  • 853
  • 9
  • 28
  • 2
    Your Screenshot() methods runs server-side - how can it possibly take a screenshot of the client? – n8wrl Jul 18 '11 at 19:55
  • 1
    Won't this only work at all if the browser is running on the same computer as the ASP server? – antlersoft Jul 18 '11 at 19:56
  • Snatch the code you have and convert to javascript :). – JonH Jul 18 '11 at 19:57
  • And even then, how would ASP.NET know? tricks with localhost? – n8wrl Jul 18 '11 at 19:58
  • @All - I did this...the only issue is it ended up being 2 apps instead of one...one was the asp.net project, and the other ended up being a tiny winforms project. It was a pain but doable...basically the winform app simply took the snapshot and updated it the information by using the same connection string back to the database. The asp.net app would get the updates being that we were tapping into the same db. It was ugly but it got the job done, and it worked much like snag it :). – JonH Jul 18 '11 at 20:01
  • It is fundamentally impossible to take a screenshot in ASP.Net. – SLaks Jul 18 '11 at 19:55

8 Answers8

7

This just won't work. Your c# code is running on the web server, not on the user's web browser and not on their computer. The only monitors that code will have access to are the monitor attached to your web server.

This probably appears to work on your development platform, but only because there your web server and your client machine are the same machine.

The best you can hope for is to include a flash or silverlight object on the page that can take the capture, and even that probably won't work.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Yea, I wasn't entirely sure it would work, but I had hoped because of all the easy to find tutorials talking about taking screen shots of ASPX pages, that it would somehow be possible. – Sivvy Jul 18 '11 at 20:03
  • In Silverlight and Flash both, you can capture the screen. However, it will only capture the Silverlight/Flash client area of the browser. In other words, only the part of the browser where the Silverlight component is running will be captured – Icemanind Jul 18 '11 at 20:09
4

Surely any code you're running here will be running on the server so will have no idea where its being displayed

In theory you could use JS to capture the DOM and then send it back to the service via an Ajax call - that's the closest you'll get I would think

Richard Blewett
  • 6,089
  • 1
  • 18
  • 23
  • The DOM was the next thing I was going to try if I found out this wasn't going to work (which I guess it really wouldn't). Thanks for the answer! – Sivvy Jul 18 '11 at 20:04
3

You're trying to take a screenshot of the client's screen from an ASP.NET application? That isn't possible. You may be able to log the entire request including the HTML then re-render it somewhere else, but you can't have that kind of interaction with the client from a server-side application framework.

WVDominick
  • 2,074
  • 15
  • 17
2

if the user moves the browser to a different monitor, I still take a shot of the Primary one

ASP.NET runs on the server, so writing server code to screen capture the browser is meaningless. You may have better luck using client-side code (javascript) to take a screenshot.

This thread uses an ActiveX solution: Take a screenshot of a webpage with JavaScript?

Community
  • 1
  • 1
Justin M. Keyes
  • 6,679
  • 3
  • 33
  • 60
2

ASP.Net WebForms do not work as you might be thinking they do. The short answer is no, you cannot do this in ASP.Net. There is no equivalent.

WinForms are displayed by interacting with the Windows operating system and are shown on the same computer's desktop. WebForms are displayed by generating HTML and sending the HTML to another computer's browser. Your ASP.Net code is not running on the other computer's browser. If your code is attempting to take a screen shot, then at-best, you will get the server's (or your computer's) display.

You can run code on the browser using JavaScript - but you still shouldn't be taking screen shots (and I don't think JavaScript will let you - at least not automatically). For one, you will be including other information from their screen which may not be related.

When ASP.Net encounters a problem, it will log the details in the Windows log on the server and also (with less detail) in the IIS log files. Consider emailing yourself an alert when an error is encountered and looking in the logs to see what went wrong.

Scott
  • 1,876
  • 17
  • 13
1

I would steal the algorithm currently used by google+. I definitely do not have the exact algorithm, or even one that is close to the actual thing, but I think this is what it's doing:

  • user clicks on 'log/report error'
  • server renders the current page using the position of the scroll bar and the size of the window (accessible through JS).
  • server renders a div covering the entire window and places the image inside of it. This div has the highest z-index.
  • when the user clicks on the div, a new div is generated that allows the user to "highlight" selected areas. The user is then allowed to attach a comment to this selected area.

It's genius in execution.

rkw
  • 7,287
  • 4
  • 26
  • 39
0

The only possiblity I can think of is something client-side that asks the browser to take a screenshot and then upload it. FogBugz does something like this with its new-case tool but that's an add-in your users would have to download and install.

n8wrl
  • 19,439
  • 4
  • 63
  • 103
0

I did this for a bug tracking app a long time ago using an ActiveX control written in --gasp-- Visual Basic. Something like that (other than the cool Google+ trick mentioned) is about your only choice.

Peter Bromberg
  • 1,498
  • 8
  • 11