//圖片轉為 base64編碼的文字
private void button1_Click(object sender, EventArgs e)
);
dlg.Multiselect = true;
dlg.Title = "選擇轉換的圖片" ;
dlg.Filter = "Image files (*.jpg;*.bmp;*.gif;*.png)|*.jpg*.jpeg;*.gif;*.bmp|AllFiles (*.png)|*.jpg*.jpeg;*.gif;*.bmp|AllFiles (*.*)|*.gif;*.bmp| .*";
if (DialogResult.OK == dlg.ShowDialog())
; i {
ToString()); }
}
}
//圖片轉為 base64編碼的文本
private void ImgToBase64String(string Imagefilename)
{
try
{
Bitmap bmp = new Bitmap(Imagefilename);
this.pictureBox1. Image = bmp;
);
MemoryStream ms = new MemoryStream();
, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
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 轉換失敗nException:" + ex.Message);
}
}
//base64編碼的文本轉為 圖片
private void button2_Click(object sender, EventArgs e)
{
dlg.Multiselect = true;
dlg.Title = "選擇要轉換的base64編碼的文字";
dlg.Filter = "txt files|*.txt";
== {
for (int i = 0; i { }
}
//base64編碼的文字轉為 圖片
private void Base64StringToImage(string txtFileName)
{
的
FileStream ifs = new FileStream(txtFileName, FileMode.Open, FileAccess.Read);
StreamReader String inputStr = sr.ReadToEnd() ;
byte[] arr = 已知.FromBase64String(inputStr);
Bitmap bmp = new Bitmap(ms);
System.Drawing.Imaging.ImageFormat.Jpeg);
//bmp.Save(FileName + ".bmp", Im .Bmp); FileName + ".gif", ImageFormat.Gif);
//bmp.Save(txtFileName + ".png", ImageFormat.Png);
ms.Close();🠜 ifs.Close();
(File.Exists(txtFileName))
{
}
//MessageBox.Show("轉換成功! ");
}
catch (Exception ex)
MessageBox.Show("Base64StringToImage 轉換失敗nException:" + ex.Message);
}
}