如何使用C#和WebKit.NET捕获完整的网站截图?

时间:2012-10-24 11:22:44

标签: c# .net webkit screenshot

我正在使用WebKit.NET在我的C#应用​​程序中集成浏览器组件。问题是我只能用截图捕获浏览器窗口中的可见部分。有没有办法捕获整个页面的屏幕截图?

3 个答案:

答案 0 :(得分:11)

使用NativeMethods.SendMessage似乎有可能,虽然这会搞乱消息队列,你可以使用http://cutycapt.sourceforge.net/http://iecapt.sourceforge.net/http://labs.awesomium.com/capturing-web-pages-with-c-net/吗?

答案 1 :(得分:7)

我改用WebBrowser; ScrollBarsEnabled = false让我捕捉整页。

以下是一些代码:

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;
}

答案 2 :(得分:4)

试试这个:

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;

    }