c# html标签table转成图片

表格生成一个真图片,并存储(如果是其他的html也可以存储图片,有正则就行)

参数SbjContent   是带 table 的html  字符串

private static Regex TableRegex = new Regex(@"<table[^>]*>[\s\S]*?<\/table>");    //匹配table标签内的字符串

    /// <summary>

    /// 一个ashx文件

    /// </summary>

    /// <param name="context"></param>

    public void ProcessRequest(HttpContext context)

    {

        try

        {

            string SbjContent = "<table class=\"grid\">内容内容内容内容内容内容内容</table>";

            string FilePathSB = "";

            foreach (Match M in Regex.Matches(SbjContent, @"width(:|=|="")(?<number>(\d{3,}(\.\d+)?))%"))

            {

                string widthNumber = M.Value.Replace(M.Groups["number"].ToString(), "100");

                SbjContent = SbjContent.Replace(M.Value, widthNumber);

            }

            MatchCollection MGroups = TableRegex.Matches(SbjContent);

            if (MGroups.Count > 0)

            {

                Bitmap m_Bitmap = WebSiteThumbnail.GetWebSiteThumbnail(MGroups[0].Value);

                MemoryStream ms = new MemoryStream();

                string SaveFilePath = HttpContext.Current.Server.MapPath("/upload/TableQuestionImage/");   //给定路径

                if (!Directory.Exists(SaveFilePath))   //判断是否存在该路径

                {

                    Directory.CreateDirectory(SaveFilePath);

                }

                string img1 = SaveFilePath + "TableQuestionZhuanImg.png";

                m_Bitmap.Save(img1);//儲存圖片

                context.Response.ClearContent(); //需要输出图象信息 要修改HTTP头  

                byte[] buffer = ms.ToArray();

                context.Response.AddHeader("Content-type", "image/jpeg");

                context.Response.BinaryWrite(buffer);

                m_Bitmap.Dispose();

                FilePathSB = "TableQuestionZhuanImg.png";

            }

            HttpContext.Current.Response.Write(FilePathSB);

        }

        catch (Exception ex)

        {

        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值