zoukankan      html  css  js  c++  java
  • html中正则匹配img

    1.正则匹配html中的img标签,取出img的url并进行图片文件下载;

     1         /// <summary>
     2         /// 将image标签的src属性的url替换为base64
     3         /// </summary>
     4         /// <param name="questionHtml"></param>
     5         /// <returns>返回替换imgurl后的questionHtml</returns>
     6         public string GetBase64ImgHtml(string questionHtml)
     7         {
     8            //获取<question></question>标签[获取某个标签时的表达式]
     9            // Regex regQuestion = new Regex(@"<question*[^<>]*?[sS]*>([sS]*)</question>", RegexOptions.IgnoreCase);
    10 
    11             string retHtml = string.Empty;
    12             //Regex regImg = new Regex(@"<img[^<>]*?src[s	
    ]*=[s	
    ]*[""']?[s	
    ]*(?<imgUrl>[^s	
    ""'<>]*)[^<>]*?/?[s	
    ]*>", RegexOptions.IgnoreCase);
    13             //去掉分组中的 s 防止图片的链接中含有空格导致匹配的url不全的问题
    14             Regex regImg = new Regex(@"<img[^<>]*?src[s	
    ]*=[s	
    ]*[""']?[s	
    ]*(?<imgUrl>[^	
    ""'<>]*)[^<>]*?/?[s	
    ]*>", RegexOptions.IgnoreCase);
    15             // 搜索匹配的字符串   
    16             MatchCollection matches = regImg.Matches(questionHtml);
    17             var list = new List<string>();
    18 
    19             // 取得匹配项列表,并逐一替换成imgUrl   
    20             foreach (Match match in matches)
    21             {
    22                 try
    23                 {
    24                     string imgUrl = match.Groups["imgUrl"].Value;
    25                     string imgType = imgUrl.Substring(imgUrl.LastIndexOf(".") + 1);
    26                     WebClient webClient = new WebClient();
    27                     Byte[] imgBytes = webClient.DownloadData(imgUrl);
    28                     string imgBase64Data = Convert.ToBase64String(imgBytes);
    29                     questionHtml = questionHtml.Replace(imgUrl, $"data:image/{imgType};base64,{imgBase64Data}");
    30                 }
    31                 catch (Exception ex)
    32                 {
    33                     continue;
    34                 }
    35             }
    36 
    37             return questionHtml;
    38         }

    通过以上方法,就可以轻松将html中img标签转换为base64;

    2.html中img标签中的base64转换为url

    其实处理的思路都是一样的,正则匹配base64 的img也都基本一致(<img[^<>]*?src[s ]*=[s ]*[""']?[s ]*(?<imgData>[^""'<>]*)[^<>]*?/?[s ]*>)。但是此处还是值得记录一下。获取到img标签中的base64 字符串后,将其转存为本地图片的过程中,部分png格式图片转存失败,在用内存流初始化 BitMap 对象的时,一直报“参数无效错误”,但是用该种方式转存Jpg图片完全OK。

     1 1.通过这种方式,部分 png 图片转存时会报错:参数无效
     2  var bytes = Convert.FromBase64String(base64Str);
     3                using(var ms = new System.IO.MemoryStream(bytes, true)){
     4                 //var bitmap = new Bitmap(ms);
     5                 var  bitmap =Image.FromStream();
     6               bitmap.Save(imgPath);
     7                bitmap.Dispose();
     8               ms.Close();
     9 }
    10                 
    11 2.直接改成将字节写入文件的方式处理;解决问题。
    12    var bytes = Convert.FromBase64String(base64Str);
    13     File.WriteAllBytes(imgPath, bytes);
  • 相关阅读:
    【Hadoop离线基础总结】Hue的简单介绍和安装部署
    【Hadoop离线基础总结】Hue与Mysql集成
    【Hadoop离线基础总结】Hue与Impala集成
    【Hadoop离线基础总结】Hue与Hive集成
    【Hadoop离线基础总结】Hue与Hadoop集成
    【Hadoop离线基础总结】impala简单介绍及安装部署
    centos7启动httpd服务失败:Job for httpd.service failed because the control process exited with error code.
    【Hadoop离线基础总结】Mac版VMware Fusion虚拟机磁盘挂载
    Azkaban无法连接网页
    【Hadoop离线基础总结】流量日志分析网站整体架构模块开发
  • 原文地址:https://www.cnblogs.com/wangyuliang/p/11776595.html
Copyright © 2011-2022 走看看