表格生成一个真图片,并存储(如果是其他的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)
{
}
}