通过 WebBrowser 获取网页截图(第二种)
2015-11-27 10:21
435 查看
这边提供第二种方式,前一种是用了webbrowser的线程安全,然后开启单线程等到webbrowser加载完成才drawimage来截图,比较稳定,这个方法在用openfiledialog打开文件后通过html的流来作为入参来截取比较稳定,如果用navigate会出现一些截取图片不完整的现象。下面上调用的函数定义:
public class HtmlThumbnail
{
private static Bitmap m_Bitmap;
private static int m_BrowserWidth = 580;
private static int m_BrowserHeight = 10;
private static string showHtml = "";
public HtmlThumbnail(string HtmlContent, int BrowserWidth, int BrowserHeight)
{
showHtml = HtmlContent;
m_BrowserHeight = BrowserHeight;
m_BrowserWidth = BrowserWidth;
}
public static Bitmap GetWebSiteThumbnail(string HtmlContent, int BrowserWidth, int BrowserHeight)
{
HtmlThumbnail thumbnailGenerator = new HtmlThumbnail(HtmlContent, BrowserWidth, BrowserHeight);
return thumbnailGenerator.GenerateWebSiteThumbnailImage();
}
public Bitmap GenerateWebSiteThumbnailImage()
{
Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
return m_Bitmap;
}
private void _GenerateWebSiteThumbnailImage()
{
System.Windows.Forms.WebBrowser m_WebBrowser = new System.Windows.Forms.WebBrowser();
m_WebBrowser.ScrollBarsEnabled = false;
m_WebBrowser.Navigate("about:blank");
m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (m_WebBrowser.ReadyState != WebBrowserReadyState.Interactive)
Application.DoEvents();
m_WebBrowser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser m_WebBrowser = (WebBrowser)sender;
<span style="color:#ff0000;"> m_WebBrowser.Document.Write(showHtml);</span>
<span style="color:#339999;">//m_WebBrowser.Navigate(showHtml);</span>
/*
m_WebBrowser.ClientSize = new Size(m_BrowserWidth, m_BrowserHeight);
m_WebBrowser.ScrollBarsEnabled = false;
m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
m_WebBrowser.BringToFront();
m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
*/
m_WebBrowser.ClientSize = new Size(m_WebBrowser.Document.Body.ScrollRectangle.Width, m_WebBrowser.Document.Body.ScrollRectangle.Height);
m_WebBrowser.ScrollBarsEnabled = false;
m_Bitmap = new Bitmap(m_WebBrowser.Document.Body.ScrollRectangle.Width, m_WebBrowser.Document.Body.ScrollRectangle.Height);
m_WebBrowser.BringToFront();
m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Document.Body.ScrollRectangle);
m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_Bitmap.Width, m_Bitmap.Height, null, IntPtr.Zero);
}
下面是打开openfiledialog然后输出html流之后来调用上面函数:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c:\\";//注意这里写路径时要用c:\\而不是c:\
openFileDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string fName = openFileDialog.FileName;
FileStream fs = new FileStream(fName, FileMode.OpenOrCreate, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.Default);
richTextBox1.Text = sr.ReadToEnd();
this.textBox2.Text = openFileDialog.FileName;
}
这个是在点击打开某个文件,包括http格式的超链接都能解析成为StreamReader流,然后通过下面两句来调用最新的函数:
webBrowser1.Navigate(this.textBox2.Text);
//FormatTableToImage(richTextBox1.Text);
FormatHtmlToImage(richTextBox1.Text);然后是FormatHtmlToImage来批量转换多个图片:
/// <summary>
/// 单个html,生成图片用
/// </summary>
/// <param name="html">匹配的html</param>
/// <param name="articleId">文章id</param>
/// <returns></returns>
private string FormatHtmlToImage(string html)
{
string articleId = "imageName";
int i = 0;
string tableHtml = html;
if (!string.IsNullOrEmpty(tableHtml))
{
string savePath = "d:\\";
Bitmap m_Bitmap = GetWebSiteThumbnail(tableHtml, 580, 10);
if (m_Bitmap != null)
{
m_Bitmap.Save("a1.jpg");
}
i++;
}
return html;
}
这边是把html或者aspx文件单独存放来转换的,如果大家觉得这样太麻烦,可以定义<table id="1">以及用<table id="1">然后用类似下面的正则来匹配出来,这样就能放在一个aspx或者html来截取图片了。
string regex1 = "<table id=\"1\"[^>]*>[\\s\\S]*</table id=\"1\">";
string regex2 = "<table id=\"2\"[^>]*>[\\s\\S]*</table id=\"2\">";
string regex3 = "<table id=\"3\"[^>]*>[\\s\\S]*</table id=\"3\">";
最后一种情形,如果不是用openfiledialog来手动打开文件或者超文本,那通过写成配置的方式也能使用,把红色的文字换成绿色就行。
public class HtmlThumbnail
{
private static Bitmap m_Bitmap;
private static int m_BrowserWidth = 580;
private static int m_BrowserHeight = 10;
private static string showHtml = "";
public HtmlThumbnail(string HtmlContent, int BrowserWidth, int BrowserHeight)
{
showHtml = HtmlContent;
m_BrowserHeight = BrowserHeight;
m_BrowserWidth = BrowserWidth;
}
public static Bitmap GetWebSiteThumbnail(string HtmlContent, int BrowserWidth, int BrowserHeight)
{
HtmlThumbnail thumbnailGenerator = new HtmlThumbnail(HtmlContent, BrowserWidth, BrowserHeight);
return thumbnailGenerator.GenerateWebSiteThumbnailImage();
}
public Bitmap GenerateWebSiteThumbnailImage()
{
Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
return m_Bitmap;
}
private void _GenerateWebSiteThumbnailImage()
{
System.Windows.Forms.WebBrowser m_WebBrowser = new System.Windows.Forms.WebBrowser();
m_WebBrowser.ScrollBarsEnabled = false;
m_WebBrowser.Navigate("about:blank");
m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (m_WebBrowser.ReadyState != WebBrowserReadyState.Interactive)
Application.DoEvents();
m_WebBrowser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser m_WebBrowser = (WebBrowser)sender;
<span style="color:#ff0000;"> m_WebBrowser.Document.Write(showHtml);</span>
<span style="color:#339999;">//m_WebBrowser.Navigate(showHtml);</span>
/*
m_WebBrowser.ClientSize = new Size(m_BrowserWidth, m_BrowserHeight);
m_WebBrowser.ScrollBarsEnabled = false;
m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
m_WebBrowser.BringToFront();
m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
*/
m_WebBrowser.ClientSize = new Size(m_WebBrowser.Document.Body.ScrollRectangle.Width, m_WebBrowser.Document.Body.ScrollRectangle.Height);
m_WebBrowser.ScrollBarsEnabled = false;
m_Bitmap = new Bitmap(m_WebBrowser.Document.Body.ScrollRectangle.Width, m_WebBrowser.Document.Body.ScrollRectangle.Height);
m_WebBrowser.BringToFront();
m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Document.Body.ScrollRectangle);
m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_Bitmap.Width, m_Bitmap.Height, null, IntPtr.Zero);
}
下面是打开openfiledialog然后输出html流之后来调用上面函数:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c:\\";//注意这里写路径时要用c:\\而不是c:\
openFileDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string fName = openFileDialog.FileName;
FileStream fs = new FileStream(fName, FileMode.OpenOrCreate, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.Default);
richTextBox1.Text = sr.ReadToEnd();
this.textBox2.Text = openFileDialog.FileName;
}
这个是在点击打开某个文件,包括http格式的超链接都能解析成为StreamReader流,然后通过下面两句来调用最新的函数:
webBrowser1.Navigate(this.textBox2.Text);
//FormatTableToImage(richTextBox1.Text);
FormatHtmlToImage(richTextBox1.Text);然后是FormatHtmlToImage来批量转换多个图片:
/// <summary>
/// 单个html,生成图片用
/// </summary>
/// <param name="html">匹配的html</param>
/// <param name="articleId">文章id</param>
/// <returns></returns>
private string FormatHtmlToImage(string html)
{
string articleId = "imageName";
int i = 0;
string tableHtml = html;
if (!string.IsNullOrEmpty(tableHtml))
{
string savePath = "d:\\";
Bitmap m_Bitmap = GetWebSiteThumbnail(tableHtml, 580, 10);
if (m_Bitmap != null)
{
m_Bitmap.Save("a1.jpg");
}
i++;
}
return html;
}
这边是把html或者aspx文件单独存放来转换的,如果大家觉得这样太麻烦,可以定义<table id="1">以及用<table id="1">然后用类似下面的正则来匹配出来,这样就能放在一个aspx或者html来截取图片了。
string regex1 = "<table id=\"1\"[^>]*>[\\s\\S]*</table id=\"1\">";
string regex2 = "<table id=\"2\"[^>]*>[\\s\\S]*</table id=\"2\">";
string regex3 = "<table id=\"3\"[^>]*>[\\s\\S]*</table id=\"3\">";
最后一种情形,如果不是用openfiledialog来手动打开文件或者超文本,那通过写成配置的方式也能使用,把红色的文字换成绿色就行。
相关文章推荐
- Android项目:proguard混淆第三方jar.
- Spring 定时器
- PHP脚本和JAVA连接mysql数据库
- 全球域名商解析新增量15强:易名中国卫冕失败
- linux防火墙操作 可以解决区域网浏览器访问linux问题
- 序列化接口:Serializable 、Parcelable
- CSS入门(8)-position和 z-index属性
- Android项目:proguard混淆第三方jar.
- Android项目:proguard混淆第三方jar.
- Android项目:proguard混淆第三方jar.
- Android项目:proguard混淆第三方jar.
- Android项目:proguard混淆第三方jar.
- android.os.NetworkOnMainThreadException错误的原因
- SSH:Hibernate框架(第一例)
- ubuntu 14.04 登录 界面 root
- 安装mysql的时候遇到的坑
- LintCode: Count 1 in Binary
- 杂
- (5.2)uboot详解——省电模式(番外)
- 《使用云计算和大数据进行性能测试》