c# 图片转Base64 Base64转图片

先将图片转成 Base64 转成图片,再将 return 回来的 base64Str 直接赋值给  Base64ToImage(string base64) 函数(图片转出来的 base64 太长 我就不贴了,记得将图片路劲换成自己图片的路径哦,GoodNight!)

  static void Main(string[] args)
        {
            // 脸部识别
            // FaceDetect.detect();
            // 图片转 base64
          string base64Str = ImageToBase64("C:/Users/Administrator/Desktop/image.png");
            // base64转 图片
           Base64ToImage(base64Str);
        }
   /// 
        /// base64 转 Image
        /// 
        /// 
        public static void Base64ToImage(string base64)
        {
            base64 = base64.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//将base64头部信息替换
            byte[] bytes = Convert.FromBase64String(base64);
            MemoryStream memStream = new MemoryStream(bytes);
            Image mImage = Image.FromStream(memStream);
            Bitmap bp = new Bitmap(mImage);
            bp.Save("C:/Users/Administrator/Desktop/" + DateTime.Now.ToString("yyyyMMddHHss") + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);//注意保存路径
        }

        /// 
        /// Image 转成 base64
        /// 
        /// 
        public static string ImageToBase64(string fileFullName)
        {
            try
            {
                Bitmap bmp = new Bitmap(fileFullName);
                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)
            {
                return null;
            }
        }

 

unity base64编码文本转换成Texture  base64编码文本转换成图片  图片转换成base64编码文本

 /// 
        /// 图片转换成base64编码文本
        /// 
        public string ImgToBase64String(string path)
        {

                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, (int)fs.Length);
                string base64String = Convert.ToBase64String(buffer);
            //    Debug.Log("获取当前图片base64为---" + base64String);
            return base64String;
        }

        /// 
        /// base64编码文本转换成Texture
        /// 

        private Texture2D Base64ToTexter2d(string Base64STR)
        {
            Texture2D pic = new Texture2D(200, 200);
            byte[] data = System.Convert.FromBase64String(Base64STR);
            //File.WriteAllBytes(Application.dataPath + "/BuildImage/Base64ToSaveImage.png", data);
            pic.LoadImage(data);
            return pic;
        }

        /// 
        /// base64编码文本转换成图片
        /// 

        public void Base64ToImg(Image imgComponent,string str)
        {
            string base64 = str;
            byte[] bytes = Convert.FromBase64String(base64);
            Texture2D tex2D = new Texture2D(100, 100);
            tex2D.LoadImage(bytes);
            Sprite s = Sprite.Create(tex2D, new Rect(0, 0, tex2D.width, tex2D.height), new Vector2(0.5f, 0.5f));
            imgComponent.sprite = s;
            Resources.UnloadUnusedAssets();
        }

 

你可能感兴趣的:(c#,unity,Base64转图片,图片转base,c#)