博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于Asp.net C#实现HTML转图片(网页快照)
阅读量:4972 次
发布时间:2019-06-12

本文共 2673 字,大约阅读时间需要 8 分钟。

一、实现方法

//WebSiteThumbnail.cs文件,在BS项目中需要添加对System.Windows.Forms的引用

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;

namespace TestWebApp

{
    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);
        }

    }

}

二、调用方法

//在任意网页中的Page_Load事件时,加入如下代码:

        protected void Page_Load(object sender, EventArgs e)

        {
            Bitmap m_Bitmap = WebSiteThumbnail.GetWebSiteThumbnail("", 600, 600, 600, 600);
            MemoryStream ms = new MemoryStream();
            m_Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//JPG、GIF、PNG等均可
            byte[] buff = ms.ToArray();
            Response.BinaryWrite(buff);
       }

转载于:https://www.cnblogs.com/q149072205/p/3670791.html

你可能感兴趣的文章
SVN版本回退
查看>>
[ubuntu]Gedit修改文件后提示无法创建备份文件同时不能保存修改过后的文件
查看>>
navicat查看mysql数据表记录数不断变化
查看>>
初探phpcms模块
查看>>
二进制日志备份与恢复,快照备份,复制
查看>>
[Leetcode] Longest Substring Without Repeating Characters
查看>>
几款KINECT应用
查看>>
《JavaScript高级程序设计》chapter 1: javascript 简介
查看>>
利用日期、经纬度求日出日落时间 C语言程序代码(zz)
查看>>
atlas制作 和 自定义字体bnfont
查看>>
一本通1604理想的正方形
查看>>
实现全屏滑动返回效果
查看>>
ios8版本地图定位注意点
查看>>
模板模式
查看>>
CM12.1/13.0编译教程
查看>>
bjoi 2008 秦腾与教学评估
查看>>
使用 IntraWeb (9) - JavaScript
查看>>
u8货位补录
查看>>
B树、B-树、B+树、B*树 红黑树
查看>>
week two(2)—Learning Rate/polynomial regression /Normal Equation
查看>>