C# Base64与图片的相互转换

/// <summary>
        /// 图片转base64
        /// </summary>
        /// <param name="Imagefilename"></param>
        /// <returns></returns>
        public string ImgToBase64(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)
            {
                return null;
            }
        }
        /// <summary>
        /// base64转图片
        /// </summary>
        /// <param name="strbase64"></param>
        public Image Base64ToImg(string strbase64)
        {
            try
            {
                byte[] bytes = Convert.FromBase64String(strbase64);
                MemoryStream memStream = new MemoryStream(bytes);
                Image img = Image.FromStream(memStream);
                return img;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

 

发布了20 篇原创文章 · 获赞 8 · 访问量 5721
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览