• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NEThtml转图片

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System.IO;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

public class WebSiteThumbnail
{
    Bitmap m_Bitmap;
    string m_Url;
    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 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 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.Navigate(m_Url);
        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.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);
    }

}


    Bitmap m_Bitmap = WebSiteThumbnail.GetWebSiteThumbnail("这里是html的连接地址", 990, 2798, 990, 2798);
    MemoryStream ms = new MemoryStream();
    m_Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//JPG、GIF、PNG等均可
    byte[] buff = ms.ToArray();
    Response.BinaryWrite(buff);

    System.Drawing.Image img = byteArrayToImage(buff);
    string url = "图片保存路径.png";
    img.Save(HttpContext.Current.Server.MapPath(url));

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Easyui+asp.netMVC系列教程第09-17节完成登录高清录制发布时间:2022-07-10
下一篇:
ASP.NET使用受保护的配置加密配置信息发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap