C # webbrowser screenshot prohibit pop-up window and Script Error prompt to intercept Flash video

Source: Internet
Author: User

First, introduce two DLL files: Microsoft. mshtml. dll and shdocvw. dll.

Approximate path:

C: \ Program Files \ Microsoft Visual Studio 10.0 \ Visual Studio Tools for office \ Pia \ common \ microsoft. mshtml. dll

C: \ windows \ system32 \ shdocvw. dll

The followingCodeThis is only done after the page is loaded. This method cannot intercept falsh, videos, and so on. It will be blank, and if you only intercept it once, it will be okay, if you want to take one screenshot every several seconds, you will find that the screenshot is exactly the same. This code is my testing code, which is easier to write for ease of use.

The sample code is as follows:

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.exe cscript ("window. Alert = NULL", "JavaScript ");
Doc.parentwindow.exe cscript ("window. Confirm = NULL", "JavaScript ");
Doc.parentwindow.exe cscript ("window. Open = NULL", "JavaScript ");
// Doc.parentwindow.exe cscript ("window. showmodaldialog = NULL", "JavaScript ");
Doc.parentwindow.exe cscript ("window. Close = NULL", "JavaScript ");
}

 

static void _ webbrowser_documentcompleted (Object sender, webbrowserdocumentcompletedeventargs e)
{< br> 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);
// Add the watermark to the position chart
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 ();
}

}

You can use the following class inherited from 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 ();
}
}

}

The following code is used to solve the problem mentioned above. This method is better and can capture dynamic videos such as falsh.

The sample code is as follows:

 

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 (); // Add the time watermark to the position Map

_ 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.exe cscript ("window. Alert = NULL", "JavaScript ");

Doc.parentwindow.exe cscript ("window. Confirm = NULL", "JavaScript ");

Doc.parentwindow.exe cscript ("window. Open = NULL", "JavaScript ");

// Doc.parentwindow.exe cscript ("window. showmodaldialog = NULL", "JavaScript ");

Doc.parentwindow.exe cscript ("window. Close = NULL", "JavaScript ");

}

Static void _ webbrowser_documentcompleted (Object sender, webbrowserdocumentcompletedeventargs E)

{

If (_ webbrowser. readystate = webbrowserreadystate. Complete)

{

Thread shot = new thread (shreadfun );

Shot. Start ();

}

}

 

 

}

 

 

 

 

 

 

 

 

 

 

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.