C# Image 转成 base64

/// 
        /// Image 转成 base64
        /// 
        /// 
        public static string ImageToBase64(string fileFullName)
        {
            Bitmap bmp = null;
            try
            {
                var suffix = Path.GetExtension(fileFullName).ToLower();
                var suffixName = suffix == ".png" ? ImageFormat.Png
                    : suffix == ".jpg" || suffix == ".jpeg"
                        ? ImageFormat.Jpeg
                        : suffix == ".bmp"
                            ? ImageFormat.Bmp
                            : suffix == ".gif"
                                ? ImageFormat.Gif
                                : ImageFormat.Jpeg;

                string result = string.Empty;
                bmp = new Bitmap(fileFullName);
                using (MemoryStream ms = new MemoryStream())
                {
                    bmp.Save(ms, suffixName);
                    byte[] arr = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(arr, 0, (int)ms.Length);
                    ms.Close();
                    result = Convert.ToBase64String(arr);
                }
                return result;
            }
            catch (Exception ex)
            {
                LogUtil.Error("ImageToBase64", ex);
                return null;
            }
            finally
            {
                if (bmp != null)
                    bmp.Dispose();
            }
        }

 

你可能感兴趣的:(C# Image 转成 base64)