How to capture a full website screenshot with C# a

2019-01-23 16:34发布

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?

3条回答
老娘就宠你
2楼-- · 2019-01-23 16:52

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;

    }
查看更多
何必那么认真
3楼-- · 2019-01-23 16:53

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;
}
查看更多
手持菜刀,她持情操
4楼-- · 2019-01-23 17:03

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/?

查看更多
登录 后发表回答