图像、图标、光标以及其他任何内容到 Base-64 转换实用工具
一个提供 GUI 将图像转换为 base-64 字符串的简单实用程序。

引言
在最近的一个项目中,我发现自己需要将几个位图图像转换为 base-64 格式。我找不到一个简单的工具来实现这个目的,所以我创建了这个非常简单的实用工具来方便我的任务。因此,虽然这篇文章只提供了一些基本信息,但其目的是简单地帮助那些可能从这种实用工具中受益的人。
此实用工具的最新版本现在支持图像、图标、光标以及任何其他数据文件。新版本提供了以下功能:
- string FileToBase64String(string path)- 请参考 ImageFromBase64String 作为反向过程的指南。
- string ImageToBase64String(Image image, ImageFormat format)
- string IconToBase64String(Icon image)
- Image ImageFromBase64String(string base64)
- Icon IconFromBase64String(string base64)
- Cursor CursorFromBase64String(string base64)
背景
图像可以存储在应用程序外部,也可以存储在某种资源文件内部。但是,有时以字符串形式存储图像是有用的。例如,当无法进行二进制传输时,或者如果图像要直接存储在源代码中时。
Using the Code
虽然该工具相当独立,但我认为解释此实用工具如何从图像转换为 base-64,以及如何反向转换会很有用。
从图像转换为 base-64 字符串
public string ImageToBase64String(Image image, ImageFormat format)
{
   MemoryStream memory = new MemoryStream();
   image.Save(memory, format);
   string base64 = Convert.ToBase64String(memory.ToArray());
   memory.Close();
   return base64;
}
从 base-64 字符串转换为图像
public Image ImageFromBase64String(string base64)
{
   MemoryStream memory = new MemoryStream(Convert.FromBase64String(base64));
   Image result = Image.FromStream(memory);
   memory.Close();
   return result;
}
历史
- 2008 年 5 月 31 日:初始发布
- 2008 年 9 月 11 日:更新版本包含对图像、图标和光标的特定支持,但它现在也可以将任何文件转换为 base-64




