获取html网页截图封装类 WebSiteThumbnail
来源:互联网 发布:软件进口销售 编辑:程序博客网 时间:2024/06/03 18:50
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(); } } }
0 0
- 获取html网页截图封装类 WebSiteThumbnail
- HTML 网页截图
- 通过 WebBrowser 获取网页截图
- 通过 WebBrowser 获取网页截图
- 把Html插入canvas实现网页截图
- Linux下使用命令行获取网页截图
- VC获取网页html
- 获取网页html
- selenium+python 封装截图类
- HTML网页截图工具开发包CutyCapt简介
- java获取网页HTML代码
- C# 获取网页html源文件
- 获取HTML网页内容SgmlReader
- 获取网页的html文本
- iOS 获取当前网页html
- 获取html网页的内容
- java获取网页html内容。。。。。。。
- 通过 WebBrowser 获取网页截图(第二种)
- 字符编码小记
- sql注入攻击学习笔记之初识sql注入
- Java Socket实现HTTP客户端来理解Session和Cookie的区别和联系
- 慕课学习笔记之过滤器篇
- oracle的背景
- 获取html网页截图封装类 WebSiteThumbnail
- Core Animation实例3-倒影效果(CAReplicatorLayer复制层)
- ssh命令使用方法
- windbg的命令类型
- 声明变量写在@interface括号中与@property的区别
- 基于ARM-contexA9按键驱动开发
- 【物联网】OpenWrt编译出现Unable to find remote helper for 'https'问题
- ble 广播包解析详说
- JavaScript函数调用及嵌套