Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12395404
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: C#/.net

2016-06-29 16:41:32

不能盲目将HTML中所有的图片转换成二进制来存储,当图片越来越大时,image的data域会变得非常大,从而导致无法通过data来读取图片

image

图片数据读不出来的现象:

image

由于很多情况是不清楚图片的,处理HTML的图片问题,还是使用相对路径来处理。

image


附将图片转为data数据的方法:

private string ReplaceFileSystemImages(string html)
{
    var matches = Regex.Matches(html, @"]*?src\s*=\s*([""']?[^'"">]+?['""])[^>]*?>", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
    foreach (Match match in matches)
    {
        string src = match.Groups[1].Value;
        src = src.Trim('\"');
        if (File.Exists(src))
        {
            var ext = Path.GetExtension(src);
            if (ext.Length > 0)
            {
                ext = ext.Substring(1);
                src = string.Format("'data:image/{0};base64,{1}'", ext, Convert.ToBase64String(File.ReadAllBytes(src)));
                html = html.Replace(match.Groups[1].Value, src);
            }
        }
    }
    return html;
}


参考文献:

阅读(2716) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~