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.Text;
namespace WebApplication3
{
public class WebSiteThumbnail
{
Bitmap m_Bitmap;
string m_Url;
string m_html = string.Empty;
public WebSiteThumbnail(string Url, string Html)
{
m_Url = Url;
m_html = Html;
}
///
/// 生成缩略图
///
/// 源码
///
public static Bitmap GetWebSiteThumbnail(string Html)
{
WebSiteThumbnail thumbnailGenerator = new WebSiteThumbnail("", Html);
ReTest:
Bitmap m_Bitmap = thumbnailGenerator.GenerateWebSiteThumbnailImage();
if (m_Bitmap == null)
{
goto ReTest;
}
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;
m_WebBrowser.DocumentText = m_html;
m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
m_WebBrowser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser m_WebBrowser = (WebBrowser)sender;
m_WebBrowser.ScrollBarsEnabled = false;
m_WebBrowser.Width = m_WebBrowser.Document.Body.ScrollRectangle.Width; ;
m_WebBrowser.Height = m_WebBrowser.Document.Body.ScrollRectangle.Height;
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_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height, null, IntPtr.Zero);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using System.Text;
namespace WebApplication3
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sbContent = new StringBuilder();
sbContent.Append("<table border="1" width="400">");
sbContent.Append("<tr>");
sbContent.Append("<td>");
sbContent.Append("标题2");
sbContent.Append("</td>");
sbContent.Append("</tr>");
sbContent.Append("<tr>");
sbContent.Append("<td >");
sbContent.Append("【环球时报综合报道】");
sbContent.Append("</td>");
sbContent.Append("</tr>");
sbContent.Append("<tr>");
sbContent.Append("<td>");
sbContent.Append("时间" + DateTime.Now.ToString());
sbContent.Append("</td>");
sbContent.Append("</tr>");
sbContent.Append("</table>");
Bitmap m_Bitmap = WebSiteThumbnail.GetWebSiteThumbnail(sbContent.ToString());
if (m_Bitmap != null)
{
MemoryStream ms = new MemoryStream();
m_Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] buff = ms.ToArray();
ms.Dispose();
ms.Close();
m_Bitmap.Dispose();
Response.ContentType = "image/jpg";
Response.BinaryWrite(buff);
Response.End();
}
}
}
}
from:http://blog.sina.com.cn/s/blog_42bea1a80101ay22.html