您的位置:首页 > Web前端 > HTML

获取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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: