• byte[] Base64 Stream 之间相互转换


    图片 base64转byte[]

    		/// <summary>
            /// 保存base64图片,返回阿里云地址
            /// </summary>
            /// <param name="imgCode"></param>
            /// <returns></returns>
            private string SaveBase64Image(string imgCode)
            {
                string imgUrl = string.Empty;
                if (!string.IsNullOrEmpty(imgCode))
                {
                    Regex reg = new Regex(@"data:(image.+);base64,(.+)");
                    if (reg.IsMatch(imgCode))
                    {
                        var matchs = reg.Match(imgCode);
                        string contentType = matchs.Groups[1].Value;
                        string base64Code = matchs.Groups[2].Value;
                        string extendType = contentType.Replace("image/", "");
                        byte[] arr = Convert.FromBase64String(base64Code);//base64转byte[]
                        //var picture = _pictureService.InsertPicture(arr, extendType);
                    }
                }
                return imgUrl;
            }
    

    Stream转byte[]

     		/// <summary>
            /// StreamToBytes
            /// </summary>
            /// <param name="stream"></param>
            /// <returns></returns>
            public static byte[] StreamToBytes(Stream stream)
            {
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                // 设置当前流的位置为流的开始
                stream.Seek(0, SeekOrigin.Begin);
                return bytes;
            }
    

    byte[]转base64

    var base64Url = Convert.ToBase64String(avatarData);//avatarData为byte[]
    

    base64转byte[]

    byte[] data = Convert.FromBase64String(signedString);
    

    file转base64

    			var requestFiles = Request.Files;
                if (requestFiles.Count > 0)
                {
                    var avatarData = CommonHelper.StreamToBytes(requestFiles[0].InputStream);//Stream转byte[]
                    var base64Url = Convert.ToBase64String(avatarData);//byte[]转base64
                }
    
  • 相关阅读:
    Something about Giraffe
    Review of TD-Leaf(lambda)
    Dancing Links and Exact Cover
    Month Scheme
    Common Bugs in C Programming
    python爬虫学习(11) —— 也写个AC自动机
    数据结构/PTA-邻接矩阵存储图的深度优先遍历/图
    SDUST 2020/数据结构/期末集合.part3
    数据结构/PTA-据后序和中序遍历输出先序遍历/树
    数据结构/PTA-PTA排名汇总/结构体快排
  • 原文地址:https://www.cnblogs.com/zhubangchao/p/7741681.html
Copyright © 2020-2023  润新知