zoukankan      html  css  js  c++  java
  • 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;
            }
        }
  • 相关阅读:
    如何写一个完整课堂管理系统(数据库增删改查)
    关于java异常处理的思考
    java03类与对象相关问题
    java02实验:方法
    java02动手动脑
    java课堂动手动脑及课后实验总结
    java测试银行系统源代码
    JAVA程序系统测试感受
    2018年第八周暑假进度报告
    2018第七周进度总结报告
  • 原文地址:https://www.cnblogs.com/zclaude/p/9838076.html
Copyright © 2011-2022 走看看