.net core 处理base64图片

目前  .Net Core上没有System.Drawing这个类库,有一些第三方类库(并未实践)
直接上代码了
   /// 
        ///  将echarts返回的base64 转成图片
        /// 
        /// 图片的base64形式
        /// 项目区分
        public void SaveImage(string image, string proname)
        {
            string path = $"{Directory.GetCurrentDirectory()}//wwwroot//Sonarqube//{proname}.png";
            string filepath = Path.GetDirectoryName(path);
            // 如果不存在就创建file文件夹
            if (!Directory.Exists(filepath))
            {
                if (filepath != null) Directory.CreateDirectory(filepath);
            }
            var match = Regex.Match(image, "data:image/png;base64,([\\w\\W]*)$");
            if (match.Success)
            {
                image = match.Groups[1].Value;
            }
            var photoBytes = Convert.FromBase64String(image);
            System.IO.File.WriteAllBytes(path, photoBytes);
        }

保存效果:
.net core 处理base64图片_第1张图片

你可能感兴趣的:(.net,core)