开发者

How do I detect which monitor an ASP.NET program is running on?

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 some开发者_如何学运维thing I can explicitly cast to a winform control?


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.


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


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.


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?


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜