侧边栏壁纸
博主头像
墨雪飘影博主等级

玫瑰是我偷的,你爱的人是我杀的,不爱你是假的。

  • 累计撰写 52 篇文章
  • 累计创建 10 个标签
  • 累计收到 21 条评论

C#图片-base64互转

墨雪飘影
2022-03-06 / 0 评论 / 0 点赞 / 153 阅读 / 2,643 字
温馨提示:
本文最后更新于 2022-03-06,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

先将图片转成 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);
        }
   /// <summary>
        /// base64 转 Image
        /// </summary>
        /// <param name="base64"></param>
        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);//注意保存路径
        }
 
        /// <summary>
        /// Image 转成 base64
        /// </summary>
        /// <param name="fileFullName"></param>
        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编码文本

 /// <summary>
        /// 图片转换成base64编码文本
        /// </summary>
        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;
        }
 
        /// <summary>
        /// base64编码文本转换成Texture
        /// </summary>
 
        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;
        }
 
        /// <summary>
        /// base64编码文本转换成图片
        /// </summary>
 
        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();
        }

原文地址:https://blog.csdn.net/qq_39097425/article/details/82890033

0

评论区