简体   繁体   English

在C#中使用屏幕捕获方法获取黑屏截图

[英]Getting black screen shot using screen capture method in c#

I am using screen capture method in my c# hands on. 我在C#上动手使用屏幕捕获方法。 In local project everything is working fine. 在本地项目中,一切正常。 But when I am publishing my application to live domain then I am getting black screen shot. 但是,当我将应用程序发布到实时域时,就会遇到黑屏的情况。

I am using below two ways for active window screen capturing 1) http://www.tutorialized.com/tutorial/How-to-capture-web-page-screenshot-in-asp.net-using-c/67952 2) http://www.c-sharpcorner.com/article/screen-capture-and-save-as-an-image/ 我使用以下两种方式进行活动窗口屏幕捕获1) http://www.tutorialized.com/tutorial/How-to-capture-web-page-screenshot-in-asp.net-using-c/67952 2) http://www.c-sharpcorner.com/article/screen-capture-and-save-as-an-image/

Above methods is working fine in local project. 上述方法在本地项目中工作正常。 But after publishing screenshot is saving into drive but getting with black color (empty). 但是,在发布屏幕快照后,屏幕截图会保存到驱动器中,但会变成黑色(空)。

Your answers will be appreciated. 您的回答将不胜感激。

The projects are intended to be used on a desktop environment, not on a web page (project on the first link is completely wrong!). 这些项目旨在在桌面环境上使用,而不是在网页上使用(第一个链接上的项目完全错误!)。

You get a black screen because you capture the screen where the application is running, so you get the screen of the server, and if there is no user logged in, the screen is black! 您会看到一个黑屏,因为您捕获了正在运行该应用程序的屏幕,因此您获得了服务器的屏幕,并且如果没有用户登录,则该屏幕为黑屏!

You can't use this code to capture remote web client screen. 您不能使用此代码捕获远程Web客户端屏幕。

try to use the WebBrowser control instead and render the result on a bitmap: 尝试改用WebBrowser控件,并在位图上呈现结果:

<asp:TextBox ID="txtUrl" runat="server" Text = "" />
<asp:Button Text="Capture" runat="server" OnClick="Capture" />
<br />
<asp:Image ID="imgScreenShot" runat="server" Height="300" Width="400" Visible = "false" />

code-behind: 后台代码:

using System.IO;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
protected void Capture(object sender, EventArgs e)
{
    string url = txtUrl.Text.Trim();
    Thread thread = new Thread(delegate()
    {
        using (WebBrowser browser = new WebBrowser())
        {
            browser.ScrollBarsEnabled = false;
            browser.AllowNavigation = true;
            browser.Navigate(url);
            browser.Width = 1024;
            browser.Height = 768;
            browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
            while (browser.ReadyState != WebBrowserReadyState.Complete)
            {
                System.Windows.Forms.Application.DoEvents();
            }
        }
    });
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
}

private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = sender as WebBrowser;
    using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height))
    {
        browser.DrawToBitmap(bitmap, new Rectangle(0, 0, browser.Width, browser.Height));
        using (MemoryStream stream = new MemoryStream())
        {
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            byte[] bytes = stream.ToArray();
            imgScreenShot.Visible = true;
            imgScreenShot.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes);
        }
    }
}

The actual question seems to be "how can I capture a screenshot using Javascript"? 实际的问题似乎是“如何使用Javascript捕获屏幕截图”? This is already answered in a previous SO question, Using HTML5/Canvas/JavaScript to take screenshots . 在上一个SO问题( 使用HTML5 / Canvas / JavaScript拍摄屏幕截图 )中已经回答了这一问题。 The answer uses the html2canvas library to read a page's DOM and render an image using Canvas. 答案使用html2canvas库读取页面的DOM并使用Canvas渲染图像。

The following code for example, will capture the current page and post the result at the page's bottom: 例如,以下代码将捕获当前页面并将结果发布在页面底部:

html2canvas(document.body, {
  onrendered: function(canvas) {
    document.body.appendChild(canvas);
  }
});

To render a different element, pass it to html2canvas instead of document.body : 要呈现其他元素,请将其传递给html2canvas而不是document.body

html2canvas(someElement,...);

Rendering an IFRAME though can be a problem, because its contents are not part of the page and hence the DOM. 但是,呈现IFRAME可能会成为问题,因为它的内容不是页面的一部分,因此也不是DOM的一部分。 Javascript simply doesn't have access to its contents. Java语言根本无法访问其内容。 This is answered in this SO question and mentioned as a limitation of html2canvas . 在这个SO问题中对此进行了回答,并提到了html2canvas的限制。

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

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