zoukankan      html  css  js  c++  java
  • C# Webbrowser截屏 禁止弹出窗口及脚本错误提示 截取flash 视频

    首先引入两个dll文件 Microsoft.mshtml.dll和SHDocVw.dll

    大概路径为

    C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Common\Microsoft.mshtml.dll

    C:\Windows\System32\SHDocVw.dll

    下面的代码只是在页面加载完成之后进行的截图,这种截图方法不能截取falsh,视频等,会出现空白的情况,而且如果你只截取一次倒无妨,如果想要每间隔几秒截取1张,你会发现截取的图片完全一样。本代码为我的测试性代码,为省事写的比较简单

    示例代码为:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.IO;
    using System.ServiceProcess;
    using System.Net;
    using System.Windows.Forms;
    using System.Drawing;
    using mshtml;
    namespace ConsoleApplication1
    {

        class ShareDeleteTest
        {
            static WebBrowser _webBrowser;
            static void Main()
            {
                Thread thread = new Thread(GenerateImage);
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                Thread.Sleep(20000);
            }
            public static void GenerateImage()
            {
                _webBrowser = new WebBrowser();
                try
                {
                    _webBrowser.ScrollBarsEnabled = true;
                    _webBrowser.ScriptErrorsSuppressed = false;
                    _webBrowser.IsWebBrowserContextMenuEnabled = false;
                    _webBrowser.ClientSize = new Size(1000, 750);
                    _webBrowser.Navigate(@"http://www.sina.com.cn/");
                    _webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(_webBrowser_DocumentCompleted);
                    (_webBrowser.ActiveXInstance as SHDocVw.WebBrowser).NavigateComplete2 += new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(ShareDeleteTest_NavigateComplete2);
                    (_webBrowser.ActiveXInstance as SHDocVw.WebBrowser).Silent = true;
                    while (_webBrowser.ReadyState != WebBrowserReadyState.Complete)
                        Application.DoEvents();

                }
                catch (Exception ex)
                {
                   
                }
                finally
                {
                    _webBrowser.Dispose();
                }
            }

            static void ShareDeleteTest_NavigateComplete2(object pDisp, ref object URL)
            {

                mshtml.IHTMLDocument2 doc = (_webBrowser.ActiveXInstance as SHDocVw.WebBrowser).Document as mshtml.IHTMLDocument2;
                doc.parentWindow.execScript("window.alert=null", "javascript");
                doc.parentWindow.execScript("window.confirm=null", "javascript");
                doc.parentWindow.execScript("window.open=null", "javascript");
                //doc.parentWindow.execScript("window.showModalDialog=null", "javascript");
                doc.parentWindow.execScript("window.close=null", "javascript");
            }

            static void _webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                WebBrowser webBrowser = (WebBrowser)sender;
                if (webBrowser.ReadyState != WebBrowserReadyState.Complete)
                    return;
                Console.WriteLine("Height:" + _webBrowser.Height);
                Console.WriteLine("Width:" + _webBrowser.Width);

                HtmlDocument document = _webBrowser.Document;
                //HtmlElement body = document.Body;
                //Rectangle rect = body.ScrollRectangle;
                //body.ScrollLeft = webBrowser.Bounds.Width;

                document.Window.ScrollTo(0, 0);
                string filename = DateTime.Now.ToString("yyyyMMddHHmmss")+".jpg";
                Bitmap _pictureBitmap = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
                webBrowser.ScrollBarsEnabled = false;
                webBrowser.DrawToBitmap(_pictureBitmap, webBrowser.Bounds);

                System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(_pictureBitmap);
                System.Drawing.Font font = new System.Drawing.Font("Calibri", 12.0f, System.Drawing.FontStyle.Regular);
                //将水印加在位图上
                graphic.DrawString(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), font, System.Drawing.Brushes.Red, new System.Drawing.PointF());
                _pictureBitmap.Save(@"G:\Picture\" + filename);
                graphic.Dispose();
                _pictureBitmap.Dispose();
            }

        }

    你可以使用下面这个继承自WebBrowser的类来截图,
        class EWebBrowser : System.Windows.Forms.WebBrowser
        {
            SHDocVw.IWebBrowser2 Iwb2;

            protected override void AttachInterfaces(object nativeActiveXObject)
            {
                Iwb2 = (SHDocVw.IWebBrowser2)nativeActiveXObject;
                Iwb2.Silent = true;
                base.AttachInterfaces(nativeActiveXObject);
            }

            protected override void DetachInterfaces()
            {
                Iwb2 = null;
                base.DetachInterfaces();
            }
        }

    }

    下面的代码转为解决上面所提出的问题,此方法更好点,可以截取动态的视频falsh等。

    示例代码如下:

    using System;

    using System.Threading;

    //using ITV.ScreenShot;

    using System.Windows.Forms;

    using System.Drawing;

    using System.Runtime.InteropServices;

    namespace ConsoleApplication1

    {

        class ShareDeleteTest

        {

            static WebBrowser _webBrowser;

            static Action deleteScreenShot;

            static void Main()

            {

                Service1 s = new Service1();

                s.StartTest();

                return;

                deleteScreenShot = ScreenShot;

                Thread thread = new Thread(GenerateImage);

                thread.SetApartmentState(ApartmentState.STA);

                thread.Start();

                Console.Read();

            }

            public static void ShreadFun()

            {

                for (int i = 0; i < 5; i++)

                {

                    _webBrowser.Invoke(deleteScreenShot);

                    Thread.Sleep(5000);

                }          

            }

            public static void ScreenShot()

            {

                HtmlDocument document = _webBrowser.Document;

                document.Window.ScrollTo(0, 200);

                _webBrowser.ScrollBarsEnabled = false;

                string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";

                Bitmap _pictureBitmap = new Bitmap(_webBrowser.Bounds.Width, _webBrowser.Bounds.Height);

                _webBrowser.DrawToBitmap(_pictureBitmap, _webBrowser.Bounds);

                Graphics graphic = Graphics.FromImage(_pictureBitmap);

                Font font = new Font("Calibri", 12.0f, System.Drawing.FontStyle.Regular);

                graphic.DrawString(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), font, System.Drawing.Brushes.Red, new System.Drawing.PointF());//将时间水印加在位图上

                _pictureBitmap.Save(@"G:\Picture\" + filename);

                graphic.Dispose();

                _pictureBitmap.Dispose();

            }

            public static void GenerateImage()

            {

                _webBrowser = new WebBrowser();

                try

                {

                    _webBrowser.ScrollBarsEnabled = true;

                    _webBrowser.ScriptErrorsSuppressed = false;

                    _webBrowser.IsWebBrowserContextMenuEnabled = false;

                    _webBrowser.ClientSize = new Size(1000, 750);

                    _webBrowser.Navigate(@"http://www.itv.cn");

                    _webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(_webBrowser_DocumentCompleted);

                    (_webBrowser.ActiveXInstance as SHDocVw.WebBrowser).NavigateComplete2 += new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(ShareDeleteTest_NavigateComplete2);

                    (_webBrowser.ActiveXInstance as SHDocVw.WebBrowser).Silent = true;

                    while (true)

                        Application.DoEvents();

                 }

                catch (Exception ex)

                {

                    Console.WriteLine(ex.Message);

                }

                finally

                {

                    //_webBrowser.Dispose();

                }

            }

            static void ShareDeleteTest_NavigateComplete2(object pDisp, ref object URL)

            {

                mshtml.IHTMLDocument2 doc = (_webBrowser.ActiveXInstance as SHDocVw.WebBrowser).Document as mshtml.IHTMLDocument2;

                doc.parentWindow.execScript("window.alert=null", "javascript");

                doc.parentWindow.execScript("window.confirm=null", "javascript");

                doc.parentWindow.execScript("window.open=null", "javascript");

                //doc.parentWindow.execScript("window.showModalDialog=null", "javascript");

                doc.parentWindow.execScript("window.close=null", "javascript");

            }

            static void _webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

            {

                if (_webBrowser.ReadyState == WebBrowserReadyState.Complete)

                {

                    Thread shot = new Thread(ShreadFun);

                    shot.Start();

                }

            }

        }

    }

  • 相关阅读:
    NND,优酷效果实在太差了
    技术人员创业的短板
    【上架通知】天轰穿.NET4趣味编程视频教程VS2010轻松学习C#零基础
    最新课程信息 课堂风格视频教程,中规中矩的教学思路设计和插诨打科的讲解方式
    Visual Studio 2010 Ultimate 中文旗舰版——提供下载地址和KEY
    整整半个月了
    学云网五一特惠活动,很喜感的图
    学员就业>我心纠结
    委托的定义和使用入门天轰穿
    第八 讲 : 流程控制循环语句 【天轰穿.Net4趣味编程系列视频教程vs2010轻松学习C#】
  • 原文地址:https://www.cnblogs.com/qq4004229/p/2304595.html
Copyright © 2011-2022 走看看