获取html网页截图封装类 WebSiteThumbnail
2016-02-03 14:19
561 查看
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; using System.Threading; using System.Windows.Forms; using System.IO; using System.Drawing.Imaging; namespace Utility { public class WebSiteThumbnail { Bitmap m_Bitmap; string m_Url; string m_Body; bool m_IsAutoHeight = false; int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight; public WebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight) { m_Url = Url; m_BrowserHeight = BrowserHeight; m_BrowserWidth = BrowserWidth; m_ThumbnailWidth = ThumbnailWidth; m_ThumbnailHeight = ThumbnailHeight; } /*public WebSiteThumbnail(string Body, int Width) { m_IsAutoHeight = true; m_Body = Body; m_BrowserWidth = Width; m_ThumbnailWidth = Width; }*/ public WebSiteThumbnail(string Url, int Width) { m_IsAutoHeight = true; m_Url = Url; m_BrowserWidth = Width; m_ThumbnailWidth = Width; } public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight) { WebSiteThumbnail thumbnailGenerator = new WebSiteThumbnail(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight); return thumbnailGenerator.GenerateWebSiteThumbnailImage(); } /* public static void GetWebSiteThumbnailToWeb(string Body, int Width) { WebSiteThumbnail thumbnailGenerator = new WebSiteThumbnail(Body, Width); Bitmap m_Bitmap = thumbnailGenerator.GenerateWebSiteThumbnailImage(); System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.BufferOutput = true; System.Web.HttpContext.Current.Response.ContentType = "image/jpeg"; //System.Web.HttpContext.Current.Response.AddHeader("Connection", "keep-alive"); m_Bitmap.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);//保存到输出流中); System.Web.HttpContext.Current.Response.Flush(); m_Bitmap.Dispose(); System.Web.HttpContext.Current.Response.End(); }*/ public static void GetWebSiteThumbnailToWeb(string Url, int Width) { WebSiteThumbnail thumbnailGenerator = new WebSiteThumbnail(Url, Width); Bitmap m_Bitmap = thumbnailGenerator.GenerateWebSiteThumbnailImage(); System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.BufferOutput = true; System.Web.HttpContext.Current.Response.ContentType = "image/jpeg"; //System.Web.HttpContext.Current.Response.AddHeader("Connection", "keep-alive"); m_Bitmap.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);//保存到输出流中); System.Web.HttpContext.Current.Response.Flush(); m_Bitmap.Dispose(); //System.Web.HttpContext.Current.Response.End(); } public static Bitmap GetWebSiteThumbnail(string Url, int Width) { WebSiteThumbnail thumbnailGenerator = new WebSiteThumbnail(Url, Width); Bitmap m_Bitmap = thumbnailGenerator.GenerateWebSiteThumbnailImage(); return m_Bitmap; } 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() { WebBrowser m_WebBrowser = new WebBrowser(); m_WebBrowser.ScrollBarsEnabled = false; //if (!string.IsNullOrWhiteSpace(m_Body)) //{ // m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted); // m_WebBrowser.Navigate("about:blank"); // m_WebBrowser.Document.Write(m_Body); // m_WebBrowser.Url = m_WebBrowser.Url; // //WebBrowser_DocumentCompleted(m_WebBrowser, null); //} //else //{ m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted); m_WebBrowser.Navigate(m_Url); while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents(); //} m_WebBrowser.Dispose(); } private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { WebBrowser m_WebBrowser = (WebBrowser)sender; if (m_IsAutoHeight) { this.m_BrowserHeight = 2000; m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight); // 获取网页高度和宽度,也可以自己设置 int height = m_WebBrowser.Document.Body.ScrollRectangle.Height; int width = m_WebBrowser.Document.Body.ScrollRectangle.Width; // 调节webBrowser的高度和宽度 m_WebBrowser.Height = height; //m_WebBrowser.Width = width; m_ThumbnailHeight = height; } else m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.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_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero); } /// <summary> /// 图片写到web /// </summary> /// <param name="bmp"></param> public static void BitmapToWeb(Bitmap bmp) { Bitmap m_Bitmap = bmp; System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.BufferOutput = true; System.Web.HttpContext.Current.Response.ContentType = "image/jpeg"; m_Bitmap.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg); System.Web.HttpContext.Current.Response.Flush(); m_Bitmap.Dispose(); } } }
相关文章推荐
- HTML 页面插入 SVG的三种方式
- Table边框的总结
- 生成openfpyxl html格式帮助文档
- html table边框
- html界面标签
- 个人笔记 html 08 浏览器返回页面时刷新
- 个人笔记 html 07 textarea 限制字数
- 个人笔记 html 06 获取验证码之后倒计时功能
- word转html(一)
- 4.MVC框架开发(母版页的应用、按钮导致的Action处理、从界面向控制器传数据和HtmlHelper控件的实现(注册的实现))
- html 5 canvas 绘制太极demo
- 个人笔记 html 05 div隐藏后仍占用空间
- 个人笔记 html 04 提交表单与跳转页面冲突时
- 自己定义html中a标签的title提示tooltip
- HTML-03表单的创建
- html 空白汉字占位符 
- htmlcleaner使用及xpath语法
- html转成pdf的方法
- HTML转义字符表的使用
- HTML string对象的操作