Base64编码在Web方面有很多应用,譬如在URL、电子邮件方面。网上有很多相关的资源用于提供Base64编码和其他编码的转换,.Net Framework也提供了现成的功能类(System.Convert)用于将二进制数据转换为Base64字符串
事出有因
我们已经做了一个编辑器,这个编辑器可以以xml格式存储一些信息。在存储图片信息时我们碰到了一些问题。我们本来在xml信息中存储的是图片的路径,然而一旦客户把这个信息copy到其他电脑上而没有同时copy相关的图片时,就会出现一些问题。
后来,我们把图片数据转换为Base64编码,替代了原先存储图片路径的方式。
转换流程
将图片转化为Base64字符串的流程是:首先使用BinaryFormatter将图片文件序列化为二进制数据,然后使用Convert类的ToBase64String方法。将Base64字符串转换为图片的流程正好相反:使用Convert类的FromBase64String得到图片文件的二进制数据,然后使用BinaryFormatter反序列化方法。
| 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 |
|
运行结果


Base64编码在Web方面有很多应用,譬如在URL、电子邮件方面。网上有很多相关的资源用于提供Base64编码和其他编码的转换,.Net Framework也提供了现成的功能类(System.Convert)用于将二进制数据转换为Base64字符串
事出有因
我们已经做了一个编辑器,这个编辑器可以以xml格式存储一些信息。在存储图片信息时我们碰到了一些问题。我们本来在xml信息中存储的是图片的路径,然而一旦客户把这个信息copy到其他电脑上而没有同时copy相关的图片时,就会出现一些问题。
后来,我们把图片数据转换为Base64编码,替代了原先存储图片路径的方式。
转换流程
将图片转化为Base64字符串的流程是:首先使用BinaryFormatter将图片文件序列化为二进制数据,然后使用Convert类的ToBase64String方法。将Base64字符串转换为图片的流程正好相反:使用Convert类的FromBase64String得到图片文件的二进制数据,然后使用BinaryFormatter反序列化方法。

/// <summary>
/// 将图片数据转换为Base64字符串
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToBase64(object sender, EventArgs e)
{
Image img = this.pictureBox.Image;
BinaryFormatter binFormatter = new BinaryFormatter();
MemoryStream memStream = new MemoryStream();
binFormatter.Serialize(memStream, img);
byte[] bytes = memStream.GetBuffer();
string base64 = Convert.ToBase64String(bytes);
this.richTextBox.Text = base64;
}
/// <summary>
/// 将Base64字符串转换为图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToImage(object sender, EventArgs e)
{
string base64 = this.richTextBox.Text;
byte[] bytes = Convert.FromBase64String(base64);
MemoryStream memStream = new MemoryStream(bytes);
BinaryFormatter binFormatter = new BinaryFormatter();
Image img = (Image)binFormatter.Deserialize(memStream);
this.pictureBox.Image = img;
}
//图片 转为 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 转换失败\nException:" + 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 转换失败\nException:" + ex.Message);
}
}
本文介绍了一种解决XML存储图片路径问题的方法:通过Base64编码将图片数据直接嵌入XML文件中,确保了数据的完整性和便携性。文章详细展示了如何使用.NET Framework中的BinaryFormatter和Convert类实现图片与Base64字符串之间的相互转换。
335

被折叠的 条评论
为什么被折叠?



