我已经编辑了这个问题以使其更容易理解。
我有一个图像文件,我必须将图像数据以二进制形式存储到现有文件中。当该文件再次在我的程序中打开时,应该以某种方式读取该二进制数据并将该图像显示在图片框中。我将如何在 C# 中执行此操作?
非常感谢任何帮助/建议。
谢谢 杰斯
编辑:
因为我们的文件结构如下:
Control
"Text here"
Location
...而且在很多情况下,同一个文件中会有不止一个或几个控件,如下所示:
Label
"This is a label"
23, 44
Label
"This is another label"
23, 64
LinkLabel
"This is a linkLabel"
23, 84
...
我不知道在哪里放置/保存以下代码:
也许在文件中是这样的...:
Image
"<controlLocationData type="Image">
<Data>
Base64 encoded image data here
</Data>
<FreeformLocation>60, 40</FreeforLocation>
</controlLocationData>"
60, 40
然后使用下面的代码来保存/加载和显示图像?...
var image = LoadBitMap("My Bitmap");
var stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
string base64Encoded = Convert.ToBase64String(stream.ToArray());
请您参考如下方法:
我可能倾向于在您的文件中创建一个代表 BMP 位的 base64 文本 block 。
编辑:
看起来您在这里已经走上了正确的轨道,我发现对于这些类型的转换,一些扩展方法非常方便...
public static string ToBase64String(this Bitmap bm)
{
MemoryStream s = new MemoryStream();
bm.Save(s, System.Drawing.Imaging.ImageFormat.Bmp);
s.Position = 0;
Byte[] bytes = new Byte[s.Length];
s.Read(bytes, 0, (int)s.Length);
return Convert.ToBase64String(bytes);
}
public static Bitmap ToBitmap(this string s)
{
Byte[] bytes = Convert.FromBase64String(s);
MemoryStream stream = new MemoryStream(bytes);
return new Bitmap(stream);
}
你的文本文件的格式没什么大不了的,你只需要能够为你的数据建立索引,所以 Xml 是一种常见的格式,但正如我所说,这只是一个找到 base64 block 的情况你在追求。
