Asp.Net获取网站截图的实例代码
程序员文章站
2024-02-29 10:59:10
复制代码 代码如下:using system;using system.collections.generic;using system.componentmodel;us...
复制代码 代码如下:
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
namespace windowsformsapplication1
{
public partial class form1 : form
{
private webbrowser _webbrowser;
public form1()
{
initializecomponent();
}
public void getthumbnail(string url)
{
_webbrowser = new webbrowser();
_webbrowser.scrollbarsenabled = false; //不显示滚动条
_webbrowser.navigate(url);
_webbrowser.documentcompleted = new webbrowserdocumentcompletedeventhandler(completed);
while (_webbrowser.readystate != webbrowserreadystate.complete)
{
system.windows.forms.application.doevents(); //避免假死,若去掉则可能无法触发 documentcompleted 事件。
}
}
public void completed(object sender, webbrowserdocumentcompletedeventargs e)
{
//设置浏览器宽度、高度为文档宽度、高度,以便截取整个网页。
_webbrowser.width = _webbrowser.document.body.scrollrectangle.width;
_webbrowser.height = _webbrowser.document.body.scrollrectangle.height;
using (bitmap bmp = new bitmap(_webbrowser.width, _webbrowser.height))
{
_webbrowser.drawtobitmap(bmp, new rectangle(0, 0, bmp.width, bmp.height));
bmp.save("capture.png", system.drawing.imaging.imageformat.png);
picturebox1.imagelocation = "capture.png";
}
}
private void button1_click(object sender, eventargs e)
{
getthumbnail(textbox1.text);
}
}
}