C#中图片与BASE64码互相转换

 1 //图片 转为    base64编码的文本
 2         private void button1_Click(object sender, EventArgs e)
 3         {
 4             OpenFileDialog dlg = new OpenFileDialog();
 5             dlg.Multiselect = true;
 6             dlg.Title = "选择要转换的图片";
 7             dlg.Filter = "Image files (*.jpg;*.bmp;*.gif;*.png)|*.jpg*.jpeg;*.gif;*.bmp|AllFiles (*.*)|*.*";
 8             if (DialogResult.OK == dlg.ShowDialog())
 9             {
10                 for (int i = 0; i < dlg.FileNames.Length; i++)
11                 {
12                     ImgToBase64String(dlg.FileNames[i].ToString());
13                 }
14             }
15         }
16         //图片 转为    base64编码的文本
17         private void ImgToBase64String(string Imagefilename)
18         {
19             try
20             {
21                 Bitmap bmp = new Bitmap(Imagefilename);
22                 this.pictureBox1.Image = bmp;
23                 FileStream fs = new FileStream(Imagefilename + ".txt", FileMode.Create);
24                 StreamWriter sw = new StreamWriter(fs);
25 
26                 MemoryStream ms = new MemoryStream();
27                 bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
28                 byte[] arr = new byte[ms.Length];
29                 ms.Position = 0;
30                 ms.Read(arr, 0, (int)ms.Length);
31                 ms.Close();
32                 String strbaser64 = Convert.ToBase64String(arr);
33                 sw.Write(strbaser64);
34 
35                 sw.Close();
36                 fs.Close();
37                // MessageBox.Show("转换成功!");
38             }
39             catch (Exception ex)
40             {
41                 MessageBox.Show("ImgToBase64String 转换失败\nException:" + ex.Message);
42             }
43         }
44 
45         //base64编码的文本 转为    图片
46         private void button2_Click(object sender, EventArgs e)
47         {
48             OpenFileDialog dlg = new OpenFileDialog();
49             dlg.Multiselect = true;
50             dlg.Title = "选择要转换的base64编码的文本";
51             dlg.Filter = "txt files|*.txt";
52             if (DialogResult.OK == dlg.ShowDialog())
53             {
54                 for (int i = 0; i < dlg.FileNames.Length; i++)
55                 {
56                     Base64StringToImage(dlg.FileNames[i].ToString());
57                 }
58                 
59             }
60         }
61         //base64编码的文本 转为    图片
62         private void Base64StringToImage(string txtFileName)
63         {
64             try
65             {
66                 FileStream ifs = new FileStream(txtFileName, FileMode.Open, FileAccess.Read);
67                 StreamReader sr = new StreamReader(ifs);
68 
69                 String inputStr = sr.ReadToEnd();
70                 byte[] arr = Convert.FromBase64String(inputStr);
71                 MemoryStream ms = new MemoryStream(arr);
72                 Bitmap bmp = new Bitmap(ms);
73 
74                 //bmp.Save(txtFileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
75                 //bmp.Save(txtFileName + ".bmp", ImageFormat.Bmp);
76                 //bmp.Save(txtFileName + ".gif", ImageFormat.Gif);
77                 //bmp.Save(txtFileName + ".png", ImageFormat.Png);
78                 ms.Close();
79                 sr.Close();
80                 ifs.Close();
81                 this.pictureBox2.Image = bmp;
82                 if (File.Exists(txtFileName))
83                 {
84                     File.Delete(txtFileName);
85                 }
86                 //MessageBox.Show("转换成功!");
87             }
88             catch (Exception ex)
89             {
90                 MessageBox.Show("Base64StringToImage 转换失败\nException:" + ex.Message);
91             }
92         }

 

posted on 2017-05-10 15:23  乐学习  阅读(556)  评论(0编辑  收藏  举报

导航