C# Base64Helper

简介: 原文:C# Base64Helper public static class Base64Helper { /// /// base64字符保存图片到本 /// /// 保存的图片完整路径 /...
原文: C# Base64Helper

public static class Base64Helper
    {
        /// <summary>
        /// base64字符保存图片到本
        /// </summary>
        /// <param name="filePath">保存的图片完整路径</param>
        /// <param name="base64String">base64字符串</param>
        public static void Base64SaveImage(string filePath, string base64String)
        {
            try
            {
                //如果base64是通过http传过来的,要注意其中的%、,、空格等转换,C#规定base64的长度必须是4的倍数,有个别语言是2的倍数,补上==。
                base64String = base64String.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
                if (base64String.Length % 4 != 0)
                {
                    base64String += "==";
                }
                byte[] arr2 = Convert.FromBase64String(base64String);
                using (MemoryStream ms2 = new MemoryStream(arr2))
                {
                    System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
                    bmp2.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    bmp2.Dispose();
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("图片保存异常:" + ex.ToString());
            }

        }

        public static string ImgToBase64String(string Imagefilename)
        {
            try
            {
                Bitmap bmp = new Bitmap(Imagefilename);

                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                return Convert.ToBase64String(arr);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("图片读取异常:" + ex.ToString());
                return null;
            }
        }

        public static string ImageToBase64(string path)
        {
            byte[] bytes = GetPictureData(path);
            string base64 = Convert.ToBase64String(bytes);
            return base64;
        }

        public static Bitmap Base64ToImage(string base64)
        {
            byte[] arr = Convert.FromBase64String(base64);
            MemoryStream ms = new MemoryStream(arr);
            Bitmap bmp = new Bitmap(ms);
            ms.Close();
            return bmp;
        }

        public static byte[] GetPictureData(string imagePath)
        {
            FileStream fs = new FileStream(imagePath, FileMode.Open);
            byte[] byData = new byte[fs.Length];
            fs.Read(byData, 0, byData.Length);
            fs.Close();
            return byData;
        }
    }

 

目录
相关文章
|
1月前
|
Linux
报错 Package ‘oniguruma‘, required by ‘virtual:world‘, not found
报错 Package ‘oniguruma‘, required by ‘virtual:world‘, not found
47 0
|
7月前
|
机器学习/深度学习 算法 数据可视化
Induction base
Induction base 是一种基于归纳推理的算法或方法,通常用于解决机器学习或数据挖掘中的问题,特别是关联规则挖掘和分类问题。其基本思想是基于一些基本的规则或假设,通过反复应用于数据来推导出更复杂的规则或结论。
15 1
|
7月前
|
JavaScript Cloud Native Go
Error: Cannot find module ‘webpack/bin/config-yargs‘ at Function.Module._resolveFilename (intern
Error: Cannot find module ‘webpack/bin/config-yargs‘ at Function.Module._resolveFilename (intern
33 0
|
12月前
|
C语言
BASE系列
BASE系列
86 0
Package ffnvcodec was not found in the pkg-config search path
Package ffnvcodec was not found in the pkg-config search path
125 0
|
Linux
编译OpenJDK8:error: control reaches end of non-void function [-Werror=return-type]
编译OpenJDK8:error: control reaches end of non-void function [-Werror=return-type]
155 0
Package sqlite3 was not found in the pkg-config search path.add the directory containing `sqlite3.pc
Package sqlite3 was not found in the pkg-config search path.add the directory containing `sqlite3.pc
143 0
|
Go 开发工具
Go1.13:使用go mod 管理依赖, 提示cannot find module providing package或cannot find main module
Go1.13:使用go mod 管理依赖, 提示cannot find module providing package或cannot find main module
555 0
Go1.13:使用go mod 管理依赖, 提示cannot find module providing package或cannot find main module
|
JavaScript PHP
Base64初探
base64是一种编码格式。
904 0
Base64初探