MyRapid MyRapidMyRapid

加密函数之Base64

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBest.Code
{
    public static class Base64
    {
        /// <summary>
        /// 将Base64转为图片文件保存并返回图片路径
        /// </summary>
        /// <param name="base64"></param>
        /// <returns></returns>
        public static string ToFile(this string base64, string fileName)
        {
            //
            byte[] arr2 = Convert.FromBase64String(base64);
            using (MemoryStream ms2 = new MemoryStream(arr2))
            {
                System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
                string guid = Guid.NewGuid().ToString() + ".png";
                if (!string.IsNullOrEmpty(fileName))
                    guid = fileName;
                bmp2.Save(guid, System.Drawing.Imaging.ImageFormat.Png);
                bmp2.Dispose();
                return guid;
            }
        }

        /// <summary>
        /// 文件转为Base64
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string ToBase64(this string filePath)
        {
            System.Drawing.Bitmap bmp1 = new System.Drawing.Bitmap(filePath);
            using (MemoryStream ms1 = new MemoryStream())
            {
                bmp1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr1 = new byte[ms1.Length];
                ms1.Position = 0;
                ms1.Read(arr1, 0, (int)ms1.Length);
                ms1.Close();
                return Convert.ToBase64String(arr1);
            }


        }

         
    }
}


创作不易,转载请保留原文连接:加密函数之Base64

点赞(137) 打赏

发表评论

热门评论

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏