# 图片转base64

gist: 代码片段 (opens new window)

//Base64字符串前缀 data:image/png;base64,
//完整base64图片 "data:image/png;base64," + Base64Str

/// <summary>
/// Base64字符串帮助类
/// </summary>
public class Base64StringHelper
{
    /// <summary>
    /// Image转Base64字符串0
    /// </summary>
    /// <param name="file">图片</param>
    /// <returns></returns>
    public string f_ConvertImageToBase64(Image pFile)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            pFile.Save(memoryStream, ImageFormat.Jpeg);
            byte[] imageBytes = memoryStream.ToArray();
            return Convert.ToBase64String(imageBytes);
        }
    }
    
    /// <summary>
    /// Image转Base64字符串1
    /// </summary>
    /// <param name="file">图片</param>
    /// <returns></returns>
    public static string ConvertImageToBase64Striing(Image file)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            //file.RawFormat会报错, 使用方法0
            file.Save(memoryStream, file.RawFormat);
            byte[] imageBytes = memoryStream.ToArray();
            return Convert.ToBase64String(imageBytes);
        }
    }
    
    /// <summary>
    /// Bitmap转base64字符串
    /// </summary>
    /// <returns></returns>
    public static string ConvertBitmapToBase64String(Bitmap bmp)
    {
        //Bitmap bmp = null;
        MemoryStream ms = null;
        try
        {
            //string Imagefilename = Application.StartupPath + @"\images\1.png";
            //bmp = new Bitmap(Imagefilename);
            ms = new MemoryStream();
            //将此图像以指定的格式保存到指定的流中。
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] buffer = new byte[ms.Length];
            //设置流的当前位置
            ms.Position = 0;
            //从当前流中读取字节块并将数据写入缓冲区。
            ms.Read(buffer, 0, (int)ms.Length);
            ms.Close();
            return Convert.ToBase64String(buffer);
        }
        catch (Exception ex)
        {
            throw;
        }
        finally
        {
            if (bmp != null)
            {
                bmp.Dispose();
            }
            if (ms != null)
            {
                ms.Dispose();
            }
        }

    }

    /// <summary>
    /// base64字符串转Bitmap
    /// </summary>
    /// <param name="base64Str">base64字符串</param>
    /// <returns></returns>
    public static Bitmap ConvertBase64StringToBitmap(string base64Str)
    {
        MemoryStream ms = null;
        Bitmap bmp = null;
        try
        {
            byte[] arr = Convert.FromBase64String(base64Str);
            ms = new MemoryStream(arr);
            bmp = new Bitmap(ms);
            //保存到本地
            //bmp.Save(@"d:\test.png", System.Drawing.Imaging.ImageFormat.Png);
            return bmp;
        }
        catch (Exception ex)
        {
            throw;
        }
        finally
        {
            if (ms != null)
            {
                ms.Close();
            }
            //返回的bmp不需要释放,否则返回为空
        }
    }

    /// <summary>
    /// base64字符串转Image
    /// </summary>
    /// <param name="base64Str">base64字符串</param>
    /// <returns></returns>
    public static Image ConvertBase64StringToImage(string base64Str)
    {
        MemoryStream ms = null;
        try
        {
            byte[] arr = Convert.FromBase64String(base64Str);
            ms = new MemoryStream(arr, 0, arr.Length);
            //ms.Write(arr, 0, arr.Length);
            //使用该数据流中嵌入的颜色管理信息
            Image image = Image.FromStream(ms, true);
            return image;
        }
        catch (Exception ex)
        {

            throw;
        }
        finally
        {
            if (ms != null)
            {
                ms.Dispose();
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Last Updated: 5/10/2024, 10:18:59 PM