简体   繁体   English

如何检测 ASP.NET 程序在哪个监视器上运行?

[英]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.我已经阅读了大量其他问题并在谷歌上搜索了这个问题,但如果我使用的是 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.我试过Screen.FromControl() ,但由于它不是一个winforms应用程序,我没有任何 System.Windows.Forms.Controls 可供它查找。

Does ASP.NET have any equivalent? ASP.NET 是否有任何等价物? Or something I can explicitly cast to a winform control?或者我可以明确地将其转换为 winform 控件?

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.您的 c# 代码在 web 服务器上运行,而不是在用户的 web 浏览器上运行,而不是在他们的计算机上运行。 The only monitors that code will have access to are the monitor attached to your web server.代码可以访问的唯一监视器是连接到 web 服务器的监视器。

This probably appears to work on your development platform, but only because there your web server and your client machine are the same machine.这可能似乎适用于您的开发平台,但这仅仅是因为您的 web 服务器和您的客户端机器是同一台机器。

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.您可以期望的最好的方法是在可以捕获的页面上包含 flash 或 silverlight object,即使这样也可能不起作用。

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从理论上讲,您可以使用 JS 捕获 DOM,然后通过 Ajax 调用将其发送回服务 - 这是我认为的最接近的

You're trying to take a screenshot of the client's screen from an ASP.NET application?您正在尝试从 ASP.NET 应用程序中截取客户端屏幕的屏幕截图? 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.您可能能够记录整个请求,包括 HTML 然后在其他地方重新呈现它,但是您不能从服务器端应用程序框架与客户端进行这种交互。

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. ASP.NET 运行在服务器上,所以写服务器代码来截屏浏览器是没有意义的。 You may have better luck using client-side code (javascript) to take a screenshot.使用客户端代码 (javascript) 截取屏幕截图可能会更好。

This thread uses an ActiveX solution: Take a screenshot of a webpage with JavaScript?此线程使用 ActiveX 解决方案: Take a screenshot of a pages with JavaScript?

ASP.Net WebForms do not work as you might be thinking they do. ASP.Net WebForms 不像您想象的那样工作。 The short answer is no, you cannot do this in ASP.Net.简短的回答是否定的,您不能在 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. WinForms 通过与 Windows 操作系统交互显示,并显示在同一台计算机的桌面上。 WebForms are displayed by generating HTML and sending the HTML to another computer's browser.通过生成 HTML 并将 HTML 发送到另一台计算机的浏览器来显示 WebForms。 Your ASP.Net code is not running on the other computer's browser.您的 ASP.Net 代码未在其他计算机的浏览器上运行。 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).您可以使用 JavaScript 在浏览器上运行代码 - 但您仍然不应该拍摄屏幕截图(而且我认为 JavaScript 不会让您 - 至少不会自动)。 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.当 ASP.Net 遇到问题时,它将在服务器上的 Windows 日志中记录详细信息,并且在 IIS 日志文件中(详细信息较少)。 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+.我会窃取 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).服务器使用滚动条的 position 和 window 的大小(可通过 JS 访问)呈现当前页面。
  • server renders a div covering the entire window and places the image inside of it.服务器渲染一个覆盖整个 window 的 div 并将图像放入其中。 This div has the highest z-index.这个 div 的 z-index 最高。
  • when the user clicks on the div, a new div is generated that allows the user to "highlight" selected areas.当用户点击 div 时,会生成一个新的 div,允许用户“突出显示”选定区域。 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. FogBugz 用它的 new-case 工具做这样的事情,但这是一个你的用户必须下载和安装的插件。

I did this for a bug tracking app a long time ago using an ActiveX control written in --gasp-- Visual Basic.很久以前,我使用 --gasp-- Visual Basic 编写的 ActiveX 控件为一个错误跟踪应用程序执行此操作。 Something like that (other than the cool Google+ trick mentioned) is about your only choice.类似的东西(除了提到的很酷的 Google+ 技巧)是你唯一的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM