C# Base64图片编码和解码

Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看RFC2045~RFC2049,上面有MIME的详细规范。
Base64编码是从二进制到字符的过程,可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码具有不可读性,需要解码后才能阅读。 (来自百度百科)

通俗来说,我们可以通过base64编码,将图片转换为二进制再转换为字符的形式进行保存,起到存储和传输的目的。

1.将图片转换为字符的形式(解码)

  //解码
        public static string GetBase64String(Bitmap image)
        {
            string UserPhoto="";
            using (MemoryStream ms1 = new MemoryStream())
            {
                image.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr1 = new byte[ms1.Length];
                ms1.Position = 0;
                ms1.Read(arr1, 0, (int)ms1.Length);
                ms1.Close();
                UserPhoto = Convert.ToBase64String(arr1);
            }
            return UserPhoto;
        }

2.将图片base64编码转换为图片(编码)

 //编码
        public static void SaveImage(string UserPhoto)
        {
            byte[] arr2 = Convert.FromBase64String(UserPhoto);
            using (MemoryStream ms2 = new MemoryStream(arr2))
            {
                System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
                bmp2.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                //bmp2.Save(filePath + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                //bmp2.Save(filePath + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                //bmp2.Save(filePath + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
                //bmp2.Save(filePath + ".png", System.Drawing.Imaging.ImageFormat.Png);
                bmp2.Dispose();
            }
        }

3. 使用案例

using System;
using System.IO;
using System.Drawing;

namespace Base64Test
{
    class Program
    {
        //图片存放位置全路径
        const string filePath = @"C:\...\Test.jpeg";        
        static void Main(string[] args)
        {
            //读图片转为Base64String
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(filePath);
            string base64string = GetBase64String(image);
            Console.WriteLine(base64string);
            Console.ReadKey();


            //将Base64String读取为图片
            //SaveImage(base64string);
            //Console.WriteLine("保存成功");
            //Console.ReadKey();
        }


        //编码
        public static void SaveImage(string UserPhoto)
        {
            byte[] arr2 = Convert.FromBase64String(UserPhoto);
            using (MemoryStream ms2 = new MemoryStream(arr2))
            {
                System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
                bmp2.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                //bmp2.Save(filePath + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                //bmp2.Save(filePath + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                //bmp2.Save(filePath + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
                //bmp2.Save(filePath + ".png", System.Drawing.Imaging.ImageFormat.Png);
                bmp2.Dispose();
            }
        }

        //解码
        public static string GetBase64String(Bitmap image)
        {
            string UserPhoto="";
            using (MemoryStream ms1 = new MemoryStream())
            {
                image.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr1 = new byte[ms1.Length];
                ms1.Position = 0;
                ms1.Read(arr1, 0, (int)ms1.Length);
                ms1.Close();
                UserPhoto = Convert.ToBase64String(arr1);
            }
            return UserPhoto;
        }
    }
}

你可能感兴趣的:(C#)