这篇文章介绍了Asp
Net获取网站截图的实例代码
有需要的朋友可以参考一下
复制代码 代码如下:
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
namespace WindowsFormsApplication
{
public partial class Form : Form
{
private WebBrowser _webBrowser;
public Form()
{
InitializeComponent();
}
public void GetThumbNail(string url)
{
_webBrowser = new WebBrowser();
_webBrowserScrollBarsEnabled = false; //不显示滚动条
_webBrowserNavigate(url);
_webBrowserDocumentCompleted = new WebBrowserDocumentCompletedEventHandler(Completed);
while (_webBrowserReadyState != WebBrowserReadyStateComplete)
{
SystemWindowsFormsApplicationDoEvents(); //避免假死若去掉则可能无法触发 DocumentCompleted 事件
}
}
public void Completed(object sender WebBrowserDocumentCompletedEventArgs e)
{
//设置浏览器宽度高度为文档宽度高度以便截取整个网页
_webBrowserWidth = _webBrowserDocumentBodyScrollRectangleWidth;
_webBrowserHeight = _webBrowserDocumentBodyScrollRectangleHeight;
using (Bitmap bmp = new Bitmap(_webBrowserWidth _webBrowserHeight))
{
_webBrowserDrawToBitmap(bmp new Rectangle( bmpWidth bmpHeight));
bmpSave("Capturepng" SystemDrawingImagingImageFormatPng);
pictureBoxImageLocation = "Capturepng";
}
}
private void button_Click(object sender EventArgs e)
{
GetThumbNail(textBoxText);
}
}
}