zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Asp.Net获取网站截图的实例代码

Net实例ASP网站代码 获取 截图
2023-06-13 09:15:03 时间
复制代码代码如下:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceWindowsFormsApplication1
{
   publicpartialclassForm1:Form
   {
       privateWebBrowser_webBrowser;
       publicForm1()
       {
           InitializeComponent();
       }
       publicvoidGetThumbNail(stringurl)
       {
           _webBrowser=newWebBrowser();
           _webBrowser.ScrollBarsEnabled=false;//不显示滚动条
           _webBrowser.Navigate(url);
           _webBrowser.DocumentCompleted=newWebBrowserDocumentCompletedEventHandler(Completed);
           while(_webBrowser.ReadyState!=WebBrowserReadyState.Complete)
           {
               System.Windows.Forms.Application.DoEvents();//避免假死,若去掉则可能无法触发DocumentCompleted事件。
           }
       }
       publicvoidCompleted(objectsender,WebBrowserDocumentCompletedEventArgse)
       {
           //设置浏览器宽度、高度为文档宽度、高度,以便截取整个网页。
           _webBrowser.Width=_webBrowser.Document.Body.ScrollRectangle.Width;
           _webBrowser.Height=_webBrowser.Document.Body.ScrollRectangle.Height;
           using(Bitmapbmp=newBitmap(_webBrowser.Width,_webBrowser.Height))
           {
               _webBrowser.DrawToBitmap(bmp,newRectangle(0,0,bmp.Width,bmp.Height));
               bmp.Save("Capture.png",System.Drawing.Imaging.ImageFormat.Png);
               pictureBox1.ImageLocation="Capture.png";
           }
       }
       privatevoidbutton1_Click(objectsender,EventArgse)
       {
           GetThumbNail(textBox1.Text);
       }
   }
}