zoukankan      html  css  js  c++  java
  • WebSiteThumbnail 直接根据html生成图片

    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

  • 相关阅读:
    关于键保留表的一些汇总
    pl/sql,Oracle数据库中,不小心delete数据并提交后如何恢复被删数据
    创建视图的with check option选项。
    oracle中update,insert,delete的高级用法
    物化视图详解--介绍、创建方法、例子
    Oracle 视图备忘
    利用替换变量提高Oracle交互性(define)
    Sqlplus中的VARIABLE以及使用VARIABLE之后使用execute。
    Unity3d 引擎原理详细介绍
    Unity 3D主要特性和缺陷
  • 原文地址:https://www.cnblogs.com/ztf_yh/p/10715260.html
Copyright © 2011-2022 走看看