展会信息港展会大全

Asp.Net获取网站截图的实例代码
来源:互联网   发布日期:2016-01-27 15:17:52   浏览:1354次  

导读:这篇文章介绍了Asp Net获取网站截图的实例代码,有需要的朋友可以参考一下复制代码 代码如下:using System;using System Collections Generic;using System ComponentModel;using System Data;using Syste ...

这篇文章介绍了Asp.Net获取网站截图的实例代码,有需要的朋友可以参考一下

复制代码 代码如下:

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);

}

}

}

赞助本站

人工智能实验室

相关热词: 截图 Net

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港