从WebBrower组件获取全页截图

问题描述 投票:0回答:2

我正在尝试使用

WebBrowser
组件捕获用户正在查看的任何网站的全页屏幕截图。

目前,我只能捕获用户在

WebBrowser
内查看的内容。但是,创建的屏幕截图图像是网页的大小。例如,下面是 BBC 网站的(半尺寸)屏幕截图,黑色区域实际上保存为透明,但我将其填充为黑色以提高可见性。

Failed screenshot of the BBC website

我见过使用新的

WebBrowser
实例来获取全页快照的解决方案。但是,我需要屏幕截图与用户当时正在查看的页面完全相同,就像全页屏幕截图在 Firefox 中的工作方式一样。

我的代码生成了上面的图像:

private void button1_Click(object sender, EventArgs e)
{

        while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
        {
            Application.DoEvents();
        }

        int scrollWidth = 0;
        int scrollHeight = 0;

        scrollHeight = webBrowser1.Document.Body.ScrollRectangle.Height;
        scrollWidth = webBrowser1.Document.Body.ScrollRectangle.Width;
        webBrowser1.Size = new Size(scrollWidth, scrollHeight);
        Bitmap bm = new Bitmap(scrollWidth, scrollHeight);

        webBrowser1.DrawToBitmap(bm, new Rectangle(0, 0, bm.Width, bm.Height));
        bm.Save(@"D:\Screenshots\test.png", ImageFormat.Png);


  }
c# .net winforms browser screenshot
2个回答
2
投票

我有一个很好用的..

 private void button1_Click(object sender, EventArgs e)
    {
       using (FileDialog fd = new SaveFileDialog())
        {
            fd.Filter = "Image (*.png)|*.png";
            if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                new WebPageSnap(webBrowser1.Url.ToString(), fd.FileName);
                //might take 3 or 4 seconds to save cauz it has to load again.
            }
        }
    }

    class WebPageSnap
    {
        WebBrowser wb;
        string outFile;

        public WebPageSnap(string url, string outputFile)
        {
            wb = new WebBrowser();
            wb.ProgressChanged += wb_ProgressChanged;
            outFile = outputFile;
            wb.ScriptErrorsSuppressed = true;
            wb.ScrollBarsEnabled = false;
            wb.Navigate(url);
        }

        void wb_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            if (e.CurrentProgress == e.MaximumProgress)
            {
                wb.ProgressChanged -= wb_ProgressChanged;
                try
                {
                    int scrollWidth = 0;
                    int scrollHeight = 0;

                    scrollHeight = wb.Document.Body.ScrollRectangle.Height;
                    scrollWidth = wb.Document.Body.ScrollRectangle.Width;
                    wb.Size = new Size(scrollWidth, scrollHeight);


                    Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
                    for (int Xcount = 0; Xcount < bitmap.Width; Xcount++)
                        for (int Ycount = 0; Ycount < bitmap.Height; Ycount++)
                            bitmap.SetPixel(Xcount, Ycount, Color.Black);
                    wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
                    bitmap.Save(outFile, ImageFormat.Png);
                }
                catch { }
            }
        }

    }

.

;Here's the result

.

enter image description here


0
投票

不工作:

System.Threading.ThreadStateException:“ActiveX 控件‘8856f961-340a-11d0-a96b-00c04fd705a2’无法实例化,因为当前线程不在单线程容器中。”

© www.soinside.com 2019 - 2024. All rights reserved.