首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用WebBrowser控件完全捕获网页

无法使用WebBrowser控件完全捕获网页
EN

Stack Overflow用户
提问于 2013-06-21 14:14:48
回答 1查看 5.4K关注 0票数 2

我正在使用WebBrowser控件将网页捕获为图像。我使用的是.NET框架和可视化C#。

我已经在MSDN论坛上发布了同样的问题以寻求帮助。

以下是URL:

http://social.msdn.microsoft.com/Forums/en-US/ab405505-f71d-4130-88e7-b43a8f3a6dce/unable-to-capture-a-web-page-fully-using-webbrowser-control

我尝试了上面的MSDN链接中的建议代码,也尝试了IECapture。

在上面所有的方法中,我正在尝试的URL的网页被部分捕获。

我使用的代码如下:

代码语言:javascript
复制
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace Capture
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

            Thread thread = new Thread(new ThreadStart(SaveWebPage2Image));
            thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join();
            while (thread.IsAlive) System.Windows.Forms.Application.DoEvents();

        }
        public void SaveWebPage2Image()
        {

            //string url = "https://qaweb1.brassring.com/JetStream/500/Presentation/Template/ASP/Candidate/Forms/ViewForm.asp?isgq=@s/8UkTuFFVU=&apprvl=@Qa8WrdFDvAg=&localeid=@IsSbRwlc1jxKpUy0SbstnA==&mode=@1yjS1Hi6/q/uNq32m6X08Q==&where=@TPEBTmFaU8Z5hrgmRqSygA==&encryptedvalues=@frA9V1Hg3yGPn/WR$lE0ZQ==*@s8G9/N7ZuBBDPNEIdP93OY7FbF6jdmLQ*@nJVB$mLvi5GYIetk2PPrKg==*@6VgXPbEuLtM=*@TxInxLRUFyv6ynZ$0AidTA==*@p$qzP$A6PVQCNoPvNsVIig==*@7jmvKabnpHc=*@y5hagH778g$HcfEEpp27ow==*@O6v$zI0Rg4x2CI21$4eh5w==*@amtcAhHDhl8=**@7jmvKabnpHc=*@s/8UkTuFFVU=";
            //string url = "https://qaweb1.brassring.com/JetStream/500/Presentation/Template/ASP/Candidate/Forms/ViewForm.asp?isgq=@7jmvKabnpHc=&apprvl=&localeid=@IsSbRwlc1jxKpUy0SbstnA==&mode=@1yjS1Hi6/q/uNq32m6X08Q==&where=@TPEBTmFaU8Z5hrgmRqSygA==&ImagePDF=true&encryptedvalues=@Uc1WQ3WL4FE=*@G6OALy2D/Sw=*@NSsHy5JAJU4=*@G6OALy2D/Sw=*@l50gSO4U$PgdNsC8UgqqPg==*@e6jizcAy9VaSMwWeuzhEpg==*@7jmvKabnpHc=*@HmtaehYz7ddx7pzp1/zn7g==*@OmlYofN7vBtVHlkn6N9HHQ==*@gkqEz1ALh58=**@s/8UkTuFFVU=*";
            string url = "https://staging.brassring.com/JetStream/500/Presentation/Template/ASP/Candidate/Forms/ViewForm.asp?isgq=@7jmvKabnpHc=&apprvl=&localeid=@IsSbRwlc1jxKpUy0SbstnA==&mode=@1yjS1Hi6/q/uNq32m6X08Q==&where=@TPEBTmFaU8Z5hrgmRqSygA==&ImagePDF=true&encryptedvalues=@beNISxIeqdbTwJDcENDNrw==*@G6OALy2D/Sw=*@RmN4OccrJCjwYUoIljGjkg==*@G6OALy2D/Sw=*@6QMkrwY3rBRhOisaSOgfLdhs5hiR$w3l*@JUzenvUf5Xc=*@7jmvKabnpHc=*@0HjV7NCQJmwZepqHbZpMsf0G5RB9W/iy*@gUULJEM726$QfVXNUh$Tpw==*@amtcAhHDhl8=**@s/8UkTuFFVU=*";

            // Load the webpage into a WebBrowser control
            WebBrowser wb;
            wb = new WebBrowser();

            wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(CaptureImage);
            wb.Navigate(url);

            while (wb.ReadyState != WebBrowserReadyState.Complete) { System.Windows.Forms.Application.DoEvents(); }

        }

        private void CaptureImage(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
                return;
            WebBrowser wb = sender as WebBrowser;
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            // Take Screenshot of the web pages full width
            //wb.Width = wb.Document.Body.ScrollRectangle.Width;
            int wbwidth =wb.Document.Body.ScrollRectangle.Width ;
            // Take Screenshot of the web pages full height
            int wbheight = wb.Document.Body.ScrollRectangle.Height;

            // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
            Bitmap bitmap = new Bitmap(wbwidth, wbheight);
            wb.Size = new Size(wbwidth, wbheight);          


            wb.DrawToBitmap(bitmap, new Rectangle(0,0,  wb.Width, wb.Height));
            //wb.Dispose();

            bitmap.Save(@"C:\web-shot.png", System.Drawing.Imaging.ImageFormat.Png);
            bitmap.Dispose();


        }
    }
}

请看输出图片:

请打开URL

Link To the Web Page

在浏览器中,将其与上面的输出图像文件进行比较,然后您将看到捕获的图像中缺少一些字段。

请调查这个问题,并帮助我解决这个问题。

如果您需要更多信息,请告诉我。

EN

回答 1

Stack Overflow用户

发布于 2013-06-21 14:26:42

下面是一个示例:

代码语言:javascript
复制
public Bitmap GenerateScreenshot(string url, int width, int height)
    {
        WebBrowser wb = new WebBrowser();
        wb.ScrollBarsEnabled = false;
        wb.ScriptErrorsSuppressed = true;
        wb.Navigate(url);
        while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }

        wb.Width = width;
        wb.Height = height;

        if (width == -1)
        {
            wb.Width = wb.Document.Body.ScrollRectangle.Width;
        }

        if (height == -1)
        {
            wb.Height = wb.Document.Body.ScrollRectangle.Height;
        }

        Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
        wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
        wb.Dispose();

        return bitmap;
    }

下面是它的使用方法:

代码语言:javascript
复制
// Generate thumbnail of a webpage at 1024x768 resolution
Bitmap thumbnail = GenerateScreenshot("http://pietschsoft.com", 1024, 768);

// Generate thumbnail of a webpage at the webpage's full size (height and width)
thumbnail = GenerateScreenshot("http://pietschsoft.com");

// Display Thumbnail in PictureBox control
pictureBox1.Image = thumbnail;

// Save Thumbnail to a File
thumbnail.Save("thumbnail.png", System.Drawing.Imaging.ImageFormat.Png);

这段代码来自http://pietschsoft.com/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17228788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档