-1

I am using screen capture method in my c# hands on. 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/

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.

Aditya Singh
  • 15,810
  • 15
  • 45
  • 67
Jatin Gadhiya
  • 1,955
  • 5
  • 23
  • 42
  • 1
    You have an XY problem. You cannot, and should not want to, take screenshots of a browser from the server. Why are you trying to do this? – CodeCaster May 11 '16 at 13:06
  • In my webpage having one iframe. and iframe coming from 3rd party site. I don't have any control in frame only I can add values in some control. so for my log i want to capture added value and store it as image. now tell me what is the best way to do it from mvc application. you can also read below answer comments for more clarification. – Jatin Gadhiya May 11 '16 at 13:10
  • Yes, I understand _what_ you are trying to do (take a screenshot), but not _why_. For all we know you're trying to capture a screenshot of a payment gateway, to access information that you can't or shouldn't access. The proper solution would be to add some identifiers to the request to the payment provider, which they in turn return in their callback to you. Or whatever. Please explain _why_ you are trying to take a screenshot, so others can suggest a more sensible approach to solving that problem. – CodeCaster May 11 '16 at 13:33
  • Dear sir, I am not trying to get screen shot of any payment provide page. I am using third party tool on my application and this third party tool providing facility using iframe. And i don't want iframe html content. – Jatin Gadhiya May 11 '16 at 13:40
  • The same still applies. Doesn't the third party tool have some kind of feedback mechanism so they can POST back the entered data to an endpoint of yours? – CodeCaster May 11 '16 at 15:50
  • Dear CodeCaster, basically data is available on my application. and this same data i am passing to iframe. and this functionality is for customer.. its their data available in my system and requirement is after passing value in iframe.. value will be automatically populated in iframe and then customer do some change on iframe only then customer want proof like they submitted information.. so that's why customer have to store image as a screenshot. – Jatin Gadhiya May 12 '16 at 05:54
  • 1
    When you try to capture a desktop on the *server's side*, you are trying to capture the *server's* desktop. Web servers have no desktops though, which is why you are getting a black image. You **can't** capture a screenshot of the user's browser with server code. – Panagiotis Kanavos May 20 '16 at 12:35
  • @PanagiotisKanavos, Thank you for your response. I understand. But is their any alternative solution for browser screenshot through my mvc application? – Jatin Gadhiya May 20 '16 at 12:41
  • 1
    @JatinGadhiya post a different question asking for what you *really* want to do. As it is, the actual question is hidden in the 6th comment and not very readable. In any case, doing *anything* on the browser involves either Javascript, a browser extension or Flash – Panagiotis Kanavos May 20 '16 at 12:43
  • Yeah.. in 6th comment its my actual requirement... can you please suggest me exact alternatives instead of javascript & flash etc. :) – Jatin Gadhiya May 20 '16 at 12:50
  • 1
    @JatinGadhiya post a *new* question. None of the original text helps in any way. In fact, you are probably asking "How can I capture a screenshot with Javascript?". If you goolge for this, one of the first answers is [this SO question - Using HTML5/Canvas/JavaScript to take screenshots](http://stackoverflow.com/questions/4912092/using-html5-canvas-javascript-to-take-screenshots/6678156) that points to the [html2canvas](https://github.com/niklasvh/html2canvas) library – Panagiotis Kanavos May 20 '16 at 12:52
  • @PanagiotisKanavos... Sorry for that.. yeah my answer showing issue. But I have already open other question but can't able to take iframe screen capture. see here. http://stackoverflow.com/questions/36724178/iframe-is-not-coming-in-screenshot-html2canvas – Jatin Gadhiya May 20 '16 at 12:55

2 Answers2

8

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.

try to use the WebBrowser control instead and render the result on a bitmap:

<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);
        }
    }
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Giox
  • 4,785
  • 8
  • 38
  • 81
  • Giox thanks for your answer. So can you please suggest me the way for capturing current screen window using web application(mvc). I have also try with html2canvas. its also working fine.. but its not supporting with iframe content. – Jatin Gadhiya May 11 '16 at 11:35
  • try the code posted in the edit, it's not MVC as I'm not very good at it but you can easily convert it – Giox May 11 '16 at 11:38
  • One more thing in my web application I am doing many things with ajax.. so if I pass url then it will not work for me. because i will loss value if page will reload. So I want whatever display in current screen it should be capture using my new ajax call. – Jatin Gadhiya May 11 '16 at 11:40
  • Giox, is their any way without passing current page url we can get active screen as a image on web application? – Jatin Gadhiya May 11 '16 at 11:48
  • You should check for a client-side solution, made in javascript like html2canvas. Search for something supporting iFrame – Giox May 11 '16 at 11:53
  • Still i didn't found any alternatives 21 days gone. You can see here. http://stackoverflow.com/questions/36724178/iframe-is-not-coming-in-screenshot-html2canvas – Jatin Gadhiya May 11 '16 at 12:02
  • so no way.Try to take two separate screenshots with html2canvas one for the whole page and one for the iframe and then combine them (using GDI) into one image, overlapping the iframe screenshot on to the web page screenshot. – Giox May 11 '16 at 12:08
  • Giox, but I don't have any control in iframe. its come from 3rd party. So how i can get iframe capture? basically i want only iframe capture. :) please advice – Jatin Gadhiya May 11 '16 at 12:11
  • 1
    are you sniffing user information?? the frame must be in the same domain. It's a security policy – Giox May 11 '16 at 14:01
  • Dear Giox, basically data is available on my application. and this same data i am passing to iframe. and this functionality is for customer.. its their data available in my system and requirement is after passing value in iframe.. value will be automatically populated in iframe and then customer do some change on iframe only then customer want proof like they submitted information.. so that's why customer have to store image as a screenshot. – Jatin Gadhiya May 11 '16 at 15:13
1

The actual question seems to be "how can I capture a screenshot using Javascript"? This is already answered in a previous SO question, Using HTML5/Canvas/JavaScript to take screenshots. The answer uses the html2canvas library to read a page's DOM and render an image using 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(someElement,...);

Rendering an IFRAME though can be a problem, because its contents are not part of the page and hence the DOM. Javascript simply doesn't have access to its contents. This is answered in this SO question and mentioned as a limitation of html2canvas.

Community
  • 1
  • 1
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236