2

我正在使用WebBrowser控件将网页捕获为图像。我正在使用 .NET Framework 和 Visual C#。

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

以下是网址:

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

我尝试使用上述 MSDN 链接中的建议代码,也尝试使用 IECCapture。

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

我正在使用的代码如下:

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();


        }
    }
}

请看输出图像:

输出图像

请打开网址

链接到网页

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

请查看此问题并帮助我解决此问题..

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

4

1 回答 1

1

这是一个示例:

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

这就是你使用它的方式:

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

于 2013-06-21T06:26:41.690 回答