wpf 将图像有效地存储在 XML 文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20630088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Store an Image efficiently in a XML file
提问by Synn
I currently writing a program that creates and fills Controls in a WPF application from a XML file. One of the features are that the User can choose a image he wants to display in the program. This image is displayed in a Image Control. After choosing the image, the program saves all data back to the XML file.
我目前正在编写一个程序,该程序从 XML 文件创建和填充 WPF 应用程序中的控件。功能之一是用户可以选择他想要在程序中显示的图像。此图像显示在图像控件中。选择图像后,程序会将所有数据保存回 XML 文件。
The image is converted and saved as follows:
图像转换并保存如下:
byte[] bytes = new byte[1];
MemoryStream ms = new MemoryStream();
System.Drawing.Image image = new Bitmap(sPathOfImage);
image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
bytes = ms.ToArray();
XElement Image = new XElement("Image", Convert.ToBase64String(bytes));
xDocument.Add(Image);
xDocument.Save("xDocument.xml");
And this works perfectly, all's fine. However, main problem is the length of the value of the XElement Image. Even for 40kb images the lenght is 60,000 signs. And for a 9mb image it needs huge 13,200,000 signs. And now I'm looking for a way better solution to store images in the XML file.
这很好用,一切都很好。但是,主要问题是 XElement Image 值的长度。即使对于 40kb 的图像,长度也是 60,000 个标志。对于 9mb 的图像,它需要 13,200,000 个巨大的标志。现在我正在寻找一种更好的解决方案来将图像存储在 XML 文件中。
Through specifications it has to be in one XML file.
通过规范,它必须在一个 XML 文件中。
So, is there a good way to make the string smaller, a more suitable stream, anything I'm missing out? Any hint is appreciated.
那么,有没有什么好方法可以使字符串更小,更合适的流,我错过了什么?任何提示表示赞赏。
采纳答案by Yaugen Vlasau
No magic will happen about filling an image content into XML file. The content will be proportional to the original file size. Even zipping the file and saving it to the XML will not help, because there are images with lower possibilities to be compressed. For evaluation purposes, I decided to find out how Visual Studio keeps the image in its resource file. The result is the following:
将图像内容填充到 XML 文件中不会发生什么神奇的事情。内容将与原始文件大小成正比。即使压缩文件并将其保存到 XML 也无济于事,因为有些图像被压缩的可能性较低。出于评估目的,我决定了解 Visual Studio 如何在其资源文件中保存图像。结果如下:
- VS puts the image under Resource folder
- in Resource.resx puts a reference to the image
- VS把图片放在Resource文件夹下
- 在 Resource.resx 中放置了对图像的引用
..
..
<data name="_myimage" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\myimage.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
Maybe would it make sense to stop reinventing the wheel, and believe the professionals? :)
也许停止重新发明轮子并相信专业人士是否有意义?:)
回答by Muhanned F.Obaid
To store image into XML file:
要将图像存储到 XML 文件中:
- Read all the bytes into memory using
File.ReadAllBytes(). - Convert the bytes to a Base64 string using
Convert.ToBase64String() - Write the Base64 Encoded string to your element content.
- 使用 将所有字节读入内存
File.ReadAllBytes()。 - 使用将字节转换为 Base64 字符串
Convert.ToBase64String() - 将 Base64 编码字符串写入您的元素内容。
To retrieve the image from XML file:
从 XML 文件中检索图像:
string val = currentXml.Element("image").Value;
byte[] bytes = Convert.FromBase64String(val);
MemoryStream mem = new MemoryStream(bytes);
Bitmap bmp2 = new Bitmap(mem);

