11

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

4

3 回答 3

12

似乎通过使用 NativeMethods.SendMessage 是可能的,虽然这会搞砸消息队列,你可以使用http://cutycapt.sourceforge.net/或者http://iecapt.sourceforge.net/http: //labs.awesomium.com/capturing-web-pages-with-c-net/ ?

于 2012-10-24T11:27:38.653 回答
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;
}
于 2012-10-24T11:42:34.370 回答
5

试试这个:

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;

    }
于 2015-07-18T15:45:58.273 回答