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 转换失败 Exception:" + 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 转换失败 Exception:" + ex.Message); 91 } 92 }
//图片 转为 base64编码的文本 private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Multiselect = true; dlg.Title = "选择要转换的图片"; dlg.Filter = "Image files (*.jpg;*.bmp;*.gif;*.png)|*.jpg*.jpeg;*.gif;*.bmp|AllFiles (*.*)|*.*"; if (DialogResult.OK == dlg.ShowDialog()) { for (int i = 0; i < dlg.FileNames.Length; i++) { ImgToBase64String(dlg.FileNames[i].ToString()); } } } //图片 转为 base64编码的文本 private void ImgToBase64String(string Imagefilename) { try { Bitmap bmp = new Bitmap(Imagefilename); this.pictureBox1.Image = bmp; FileStream fs = new FileStream(Imagefilename + ".txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs);
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(); String strbaser64 = Convert.ToBase64String(arr); sw.Write(strbaser64);
sw.Close(); fs.Close(); // MessageBox.Show("转换成功!"); } catch (Exception ex) { MessageBox.Show("ImgToBase64String 转换失败
Exception:" + ex.Message); } }
//base64编码的文本 转为 图片 private void button2_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Multiselect = true; dlg.Title = "选择要转换的base64编码的文本"; dlg.Filter = "txt files|*.txt"; if (DialogResult.OK == dlg.ShowDialog()) { for (int i = 0; i < dlg.FileNames.Length; i++) { Base64StringToImage(dlg.FileNames[i].ToString()); } } } //base64编码的文本 转为 图片 private void Base64StringToImage(string txtFileName) { try { FileStream ifs = new FileStream(txtFileName, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(ifs);
String inputStr = sr.ReadToEnd(); byte[] arr = Convert.FromBase64String(inputStr); MemoryStream ms = new MemoryStream(arr); Bitmap bmp = new Bitmap(ms);
//bmp.Save(txtFileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //bmp.Save(txtFileName + ".bmp", ImageFormat.Bmp); //bmp.Save(txtFileName + ".gif", ImageFormat.Gif); //bmp.Save(txtFileName + ".png", ImageFormat.Png); ms.Close(); sr.Close(); ifs.Close(); this.pictureBox2.Image = bmp; if (File.Exists(txtFileName)) { File.Delete(txtFileName); } //MessageBox.Show("转换成功!"); } catch (Exception ex) { MessageBox.Show("Base64StringToImage 转换失败
Exception:" + ex.Message); } }