How to capture a full website screenshot with C# and WebKit.NET?

21,315

Solution 1

Seems that it is kind of possible by using NativeMethods.SendMessage, although this can screw up the message queue, could you use http://cutycapt.sourceforge.net/ or perhaps http://iecapt.sourceforge.net/ or http://labs.awesomium.com/capturing-web-pages-with-c-net/?

Solution 2

I use WebBrowser instead; ScrollBarsEnabled = false let me capture whole page.

Here is some code:

protected override void Render(HtmlTextWriter writer)
 {

        StringBuilder builder = new StringBuilder();
        HtmlTextWriter htw = new HtmlTextWriter(new StringWriter(builder));
        base.Render(htw);
        string html = builder.ToString();

        _Generate(html);
 } 


private void _Generate(string html)
{
    var browser = new WebBrowser { ScrollBarsEnabled = false };
    DisplayHtml(html, browser);
    browser.DocumentCompleted += WebBrowser_DocumentCompleted;
    while (browser.ReadyState != WebBrowserReadyState.Complete)
       Application.DoEvents();  
    browser.Dispose();
}

private void DisplayHtml(string html, WebBrowser browser)
{
    browser.Navigate("about:blank");
    if (browser.Document != null)
    {
        browser.Document.Write(string.Empty);
    }
    browser.DocumentText = html;
}

Solution 3

Try This:

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Forms;

    public byte[] picbytes;

    private void capture_Click(object sender, EventArgs e)
    {

        makepicture(txtURL.Text.Trim());
        pictureBox1.Visible = true;
        pictureBox1.Image = ByteToImage(picbytes);
    }

    private void makepicture(string url)
    {
        Thread thread = new Thread(delegate()
        {
            using (WebBrowser browser = new WebBrowser())
            {
                browser.ScrollBarsEnabled = false;
                browser.AllowNavigation = true;
                browser.Navigate(url);
                browser.Width = 1024;
                browser.Height = 1575;
                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 System.Drawing.Rectangle(0, 0, browser.Width, browser.Height));
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                byte[] bytes = stream.ToArray();
                picbytes = bytes;

            }
        }
    }

    public static Bitmap ByteToImage(byte[] blob)
    {
        MemoryStream mStream = new MemoryStream();
        byte[] pData = blob;
        mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
        Bitmap bm = new Bitmap(mStream, false);
        mStream.Dispose();
        return bm;

    }
Share:
21,315
topless
Author by

topless

Programmer by day, Superman by night.

Updated on July 19, 2020

Comments

  • topless
    topless almost 4 years

    I am using WebKit.NET to integrate a browser component in my C# application. The problem is I can only capture the visible part in the browser window with a screenshot. Is there a way to capture the screenshot of the whole page?

  • topless
    topless over 11 years
    I have stumbled several solutions using the WebBrowser component, but I am interested in using the WebKit engine for capturing it.
  • byako
    byako almost 9 years
    Could you please indent your code? ('Edit', select everything, Ctrl-k). Thanks.
  • Chizl
    Chizl about 8 years
    This works perfectly, with one flaw.. You really need to know the dimensions to set the browser control, because it will only print what's within. I guess it's now a search to find, how to tell how wide and how high a web page is.
  • Chizl
    Chizl about 8 years
    within DocumentCompleted() add the following, right after creating the browser object. int height = browser.Document.Body.ScrollRectangle.Height; int width = browser.Document.Body.ScrollRectangle.Width; browser.Height = height; browser.Width = width;